test/teststreaming.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@5254
    12
/********************************************************************************
slouken@5254
    13
 *                                                                              *
slouken@5254
    14
 * Running moose :) Coded by Mike Gorchak.                                      *
slouken@5254
    15
 *                                                                              *
slouken@5254
    16
 ********************************************************************************/
slouken@5254
    17
slouken@5254
    18
#include <stdlib.h>
slouken@5254
    19
#include <stdio.h>
slouken@5254
    20
icculus@9278
    21
#ifdef __EMSCRIPTEN__
icculus@9278
    22
#include <emscripten/emscripten.h>
icculus@9278
    23
#endif
icculus@9278
    24
slouken@5254
    25
#include "SDL.h"
slouken@5254
    26
slouken@5254
    27
#define MOOSEPIC_W 64
slouken@5254
    28
#define MOOSEPIC_H 88
slouken@5254
    29
slouken@5254
    30
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
slouken@5254
    31
#define MOOSEFRAMES_COUNT 10
slouken@5254
    32
slouken@5254
    33
SDL_Color MooseColors[84] = {
philipp@9101
    34
    {49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255},
philipp@9101
    35
    {66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255},
philipp@9101
    36
    {82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255},
philipp@9101
    37
    {90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255},
philipp@9101
    38
    {99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255},
philipp@9101
    39
    {115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255},
philipp@9101
    40
    {115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255},
philipp@9101
    41
    {132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255},
philipp@9101
    42
    {132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255},
philipp@9101
    43
    {140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255},
philipp@9101
    44
    {148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255},
philipp@9101
    45
    {156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255},
philipp@9101
    46
    {165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255},
philipp@9101
    47
    {165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255},
philipp@9101
    48
    {173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255},
philipp@9101
    49
    {181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255},
philipp@9101
    50
    {189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255},
philipp@9101
    51
    {189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255},
philipp@9101
    52
    {206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255},
philipp@9101
    53
    {214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255},
philipp@9101
    54
    {222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255}
slouken@5254
    55
};
slouken@5254
    56
slouken@5254
    57
Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
slouken@5254
    58
icculus@9278
    59
SDL_Renderer *renderer;
icculus@9278
    60
int frame;
icculus@9278
    61
SDL_Texture *MooseTexture;
icculus@9278
    62
SDL_bool done = SDL_FALSE;
icculus@9278
    63
slouken@5255
    64
void quit(int rc)
slouken@5254
    65
{
slouken@5254
    66
    SDL_Quit();
slouken@5254
    67
    exit(rc);
slouken@5254
    68
}
slouken@5254
    69
slouken@5255
    70
void UpdateTexture(SDL_Texture *texture, int frame)
slouken@5254
    71
{
slouken@5254
    72
    SDL_Color *color;
slouken@5254
    73
    Uint8 *src;
slouken@5254
    74
    Uint32 *dst;
slouken@5254
    75
    int row, col;
slouken@5254
    76
    void *pixels;
slouken@5254
    77
    int pitch;
slouken@5254
    78
slouken@5254
    79
    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
aschiffler@7639
    80
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
slouken@5254
    81
        quit(5);
slouken@5254
    82
    }
slouken@5254
    83
    src = MooseFrames[frame];
slouken@5254
    84
    for (row = 0; row < MOOSEPIC_H; ++row) {
slouken@5254
    85
        dst = (Uint32*)((Uint8*)pixels + row * pitch);
slouken@5254
    86
        for (col = 0; col < MOOSEPIC_W; ++col) {
slouken@5254
    87
            color = &MooseColors[*src++];
slouken@5254
    88
            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
slouken@5254
    89
        }
slouken@5254
    90
    }
slouken@5254
    91
    SDL_UnlockTexture(texture);
slouken@5254
    92
}
slouken@5254
    93
icculus@9278
    94
void
icculus@9278
    95
loop()
icculus@9278
    96
{
icculus@9278
    97
    SDL_Event event;
icculus@9278
    98
icculus@9278
    99
    while (SDL_PollEvent(&event)) {
icculus@9278
   100
        switch (event.type) {
icculus@9278
   101
        case SDL_KEYDOWN:
icculus@9278
   102
            if (event.key.keysym.sym == SDLK_ESCAPE) {
icculus@9278
   103
                done = SDL_TRUE;
icculus@9278
   104
            }
icculus@9278
   105
            break;
icculus@9278
   106
        case SDL_QUIT:
icculus@9278
   107
            done = SDL_TRUE;
icculus@9278
   108
            break;
icculus@9278
   109
        }
icculus@9278
   110
    }
icculus@9278
   111
icculus@9278
   112
    frame = (frame + 1) % MOOSEFRAMES_COUNT;
icculus@9278
   113
    UpdateTexture(MooseTexture, frame);
icculus@9278
   114
icculus@9278
   115
    SDL_RenderClear(renderer);
icculus@9278
   116
    SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
icculus@9278
   117
    SDL_RenderPresent(renderer);
philipp@9607
   118
philipp@9607
   119
#ifdef __EMSCRIPTEN__
philipp@9607
   120
    if (done) {
philipp@9607
   121
        emscripten_cancel_main_loop();
philipp@9607
   122
    }
philipp@9607
   123
#endif
icculus@9278
   124
}
icculus@9278
   125
slouken@5254
   126
int
slouken@5254
   127
main(int argc, char **argv)
slouken@5254
   128
{
slouken@5254
   129
    SDL_Window *window;
slouken@5254
   130
    SDL_RWops *handle;
slouken@5254
   131
philipp@9922
   132
    /* Enable standard application logging */
aschiffler@7639
   133
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   134
slouken@5254
   135
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
aschiffler@7639
   136
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@5254
   137
        return 1;
slouken@5254
   138
    }
slouken@5254
   139
slouken@5254
   140
    /* load the moose images */
slouken@5254
   141
    handle = SDL_RWFromFile("moose.dat", "rb");
slouken@5254
   142
    if (handle == NULL) {
aschiffler@7639
   143
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
slouken@5254
   144
        quit(2);
slouken@5254
   145
    }
slouken@5254
   146
    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
slouken@5254
   147
    SDL_RWclose(handle);
slouken@5254
   148
slouken@5254
   149
slouken@5254
   150
    /* Create the window and renderer */
slouken@5254
   151
    window = SDL_CreateWindow("Happy Moose",
slouken@5254
   152
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   153
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   154
                              MOOSEPIC_W*4, MOOSEPIC_H*4,
philipp@7478
   155
                              SDL_WINDOW_RESIZABLE);
slouken@5254
   156
    if (!window) {
aschiffler@7639
   157
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
slouken@5254
   158
        quit(3);
slouken@5254
   159
    }
slouken@5254
   160
slouken@5254
   161
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5254
   162
    if (!renderer) {
aschiffler@7639
   163
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
slouken@5254
   164
        quit(4);
slouken@5254
   165
    }
slouken@5254
   166
slouken@5254
   167
    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
slouken@5254
   168
    if (!MooseTexture) {
aschiffler@7639
   169
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
slouken@5254
   170
        quit(5);
slouken@5254
   171
    }
slouken@5254
   172
slouken@5254
   173
    /* Loop, waiting for QUIT or the escape key */
slouken@5254
   174
    frame = 0;
icculus@9278
   175
icculus@9278
   176
#ifdef __EMSCRIPTEN__
icculus@9278
   177
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   178
#else
slouken@5254
   179
    while (!done) {
icculus@9278
   180
        loop();
slouken@5254
   181
        }
icculus@9278
   182
#endif
slouken@5254
   183
slouken@5254
   184
    SDL_DestroyRenderer(renderer);
slouken@5254
   185
slouken@5254
   186
    quit(0);
slouken@5254
   187
    return 0;
slouken@5254
   188
}
slouken@5254
   189
slouken@5254
   190
/* vi: set ts=4 sw=4 expandtab: */