test/teststreaming.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5255 b0f5108fda60
child 7478 0d02f6a323f5
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /********************************************************************************
    13  *                                                                              *
    14  * Running moose :) Coded by Mike Gorchak.                                      *
    15  *                                                                              *
    16  ********************************************************************************/
    17 
    18 #include <stdlib.h>
    19 #include <stdio.h>
    20 
    21 #include "SDL.h"
    22 
    23 #define MOOSEPIC_W 64
    24 #define MOOSEPIC_H 88
    25 
    26 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    27 #define MOOSEFRAMES_COUNT 10
    28 
    29 SDL_Color MooseColors[84] = {
    30     {49, 49, 49}, {66, 24, 0}, {66, 33, 0}, {66, 66, 66},
    31     {66, 115, 49}, {74, 33, 0}, {74, 41, 16}, {82, 33, 8},
    32     {82, 41, 8}, {82, 49, 16}, {82, 82, 82}, {90, 41, 8},
    33     {90, 41, 16}, {90, 57, 24}, {99, 49, 16}, {99, 66, 24},
    34     {99, 66, 33}, {99, 74, 33}, {107, 57, 24}, {107, 82, 41},
    35     {115, 57, 33}, {115, 66, 33}, {115, 66, 41}, {115, 74, 0},
    36     {115, 90, 49}, {115, 115, 115}, {123, 82, 0}, {123, 99, 57},
    37     {132, 66, 41}, {132, 74, 41}, {132, 90, 8}, {132, 99, 33},
    38     {132, 99, 66}, {132, 107, 66}, {140, 74, 49}, {140, 99, 16},
    39     {140, 107, 74}, {140, 115, 74}, {148, 107, 24}, {148, 115, 82},
    40     {148, 123, 74}, {148, 123, 90}, {156, 115, 33}, {156, 115, 90},
    41     {156, 123, 82}, {156, 132, 82}, {156, 132, 99}, {156, 156, 156},
    42     {165, 123, 49}, {165, 123, 90}, {165, 132, 82}, {165, 132, 90},
    43     {165, 132, 99}, {165, 140, 90}, {173, 132, 57}, {173, 132, 99},
    44     {173, 140, 107}, {173, 140, 115}, {173, 148, 99}, {173, 173, 173},
    45     {181, 140, 74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
    46     {189, 148, 123}, {189, 156, 82}, {189, 156, 123}, {189, 156, 132},
    47     {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165, 99},
    48     {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
    49     {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
    50     {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
    51 };
    52 
    53 Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
    54 
    55 void quit(int rc)
    56 {
    57     SDL_Quit();
    58     exit(rc);
    59 }
    60 
    61 void UpdateTexture(SDL_Texture *texture, int frame)
    62 {
    63     SDL_Color *color;
    64     Uint8 *src;
    65     Uint32 *dst;
    66     int row, col;
    67     void *pixels;
    68     int pitch;
    69 
    70     if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
    71         fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
    72         quit(5);
    73     }
    74     src = MooseFrames[frame];
    75     for (row = 0; row < MOOSEPIC_H; ++row) {
    76         dst = (Uint32*)((Uint8*)pixels + row * pitch);
    77         for (col = 0; col < MOOSEPIC_W; ++col) {
    78             color = &MooseColors[*src++];
    79             *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
    80         }
    81     }
    82     SDL_UnlockTexture(texture);
    83 }
    84 
    85 int
    86 main(int argc, char **argv)
    87 {
    88     SDL_Window *window;
    89     SDL_Renderer *renderer;
    90     SDL_RWops *handle;
    91     SDL_Texture *MooseTexture;
    92     SDL_Event event;
    93     SDL_bool done = SDL_FALSE;
    94     int frame;
    95 
    96     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    97         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    98         return 1;
    99     }
   100 
   101     /* load the moose images */
   102     handle = SDL_RWFromFile("moose.dat", "rb");
   103     if (handle == NULL) {
   104         fprintf(stderr, "Can't find the file moose.dat !\n");
   105         quit(2);
   106     }
   107     SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   108     SDL_RWclose(handle);
   109 
   110 
   111     /* Create the window and renderer */
   112     window = SDL_CreateWindow("Happy Moose",
   113                               SDL_WINDOWPOS_UNDEFINED,
   114                               SDL_WINDOWPOS_UNDEFINED,
   115                               MOOSEPIC_W*4, MOOSEPIC_H*4,
   116                               SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
   117     if (!window) {
   118         fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   119         quit(3);
   120     }
   121 
   122     renderer = SDL_CreateRenderer(window, -1, 0);
   123     if (!renderer) {
   124         fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   125         quit(4);
   126     }
   127 
   128     MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   129     if (!MooseTexture) {
   130         fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   131         quit(5);
   132     }
   133 
   134     /* Loop, waiting for QUIT or the escape key */
   135     frame = 0;
   136     while (!done) {
   137         while (SDL_PollEvent(&event)) {
   138             switch (event.type) {
   139             case SDL_KEYDOWN:
   140                 if (event.key.keysym.sym == SDLK_ESCAPE) {
   141                     done = SDL_TRUE;
   142                 }
   143                 break;
   144             case SDL_QUIT:
   145                 done = SDL_TRUE;
   146                 break;
   147             }
   148         }
   149 
   150         frame = (frame + 1) % MOOSEFRAMES_COUNT;
   151         UpdateTexture(MooseTexture, frame);
   152 
   153         SDL_RenderClear(renderer);
   154         SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
   155         SDL_RenderPresent(renderer);
   156     }
   157     SDL_DestroyRenderer(renderer);
   158 
   159     quit(0);
   160     return 0;
   161 }
   162 
   163 /* vi: set ts=4 sw=4 expandtab: */