test/teststreaming.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 5535 96594ac5fd1a
child 7478 0d02f6a323f5
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 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
slouken@5254
    21
#include "SDL.h"
slouken@5254
    22
slouken@5254
    23
#define MOOSEPIC_W 64
slouken@5254
    24
#define MOOSEPIC_H 88
slouken@5254
    25
slouken@5254
    26
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
slouken@5254
    27
#define MOOSEFRAMES_COUNT 10
slouken@5254
    28
slouken@5254
    29
SDL_Color MooseColors[84] = {
slouken@5255
    30
    {49, 49, 49}, {66, 24, 0}, {66, 33, 0}, {66, 66, 66},
slouken@5255
    31
    {66, 115, 49}, {74, 33, 0}, {74, 41, 16}, {82, 33, 8},
slouken@5255
    32
    {82, 41, 8}, {82, 49, 16}, {82, 82, 82}, {90, 41, 8},
slouken@5255
    33
    {90, 41, 16}, {90, 57, 24}, {99, 49, 16}, {99, 66, 24},
slouken@5255
    34
    {99, 66, 33}, {99, 74, 33}, {107, 57, 24}, {107, 82, 41},
slouken@5255
    35
    {115, 57, 33}, {115, 66, 33}, {115, 66, 41}, {115, 74, 0},
slouken@5255
    36
    {115, 90, 49}, {115, 115, 115}, {123, 82, 0}, {123, 99, 57},
slouken@5255
    37
    {132, 66, 41}, {132, 74, 41}, {132, 90, 8}, {132, 99, 33},
slouken@5255
    38
    {132, 99, 66}, {132, 107, 66}, {140, 74, 49}, {140, 99, 16},
slouken@5255
    39
    {140, 107, 74}, {140, 115, 74}, {148, 107, 24}, {148, 115, 82},
slouken@5255
    40
    {148, 123, 74}, {148, 123, 90}, {156, 115, 33}, {156, 115, 90},
slouken@5255
    41
    {156, 123, 82}, {156, 132, 82}, {156, 132, 99}, {156, 156, 156},
slouken@5255
    42
    {165, 123, 49}, {165, 123, 90}, {165, 132, 82}, {165, 132, 90},
slouken@5255
    43
    {165, 132, 99}, {165, 140, 90}, {173, 132, 57}, {173, 132, 99},
slouken@5255
    44
    {173, 140, 107}, {173, 140, 115}, {173, 148, 99}, {173, 173, 173},
slouken@5255
    45
    {181, 140, 74}, {181, 148, 115}, {181, 148, 123}, {181, 156, 107},
slouken@5255
    46
    {189, 148, 123}, {189, 156, 82}, {189, 156, 123}, {189, 156, 132},
slouken@5255
    47
    {189, 189, 189}, {198, 156, 123}, {198, 165, 132}, {206, 165, 99},
slouken@5255
    48
    {206, 165, 132}, {206, 173, 140}, {206, 206, 206}, {214, 173, 115},
slouken@5255
    49
    {214, 173, 140}, {222, 181, 148}, {222, 189, 132}, {222, 189, 156},
slouken@5255
    50
    {222, 222, 222}, {231, 198, 165}, {231, 231, 231}, {239, 206, 173}
slouken@5254
    51
};
slouken@5254
    52
slouken@5254
    53
Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
slouken@5254
    54
slouken@5255
    55
void quit(int rc)
slouken@5254
    56
{
slouken@5254
    57
    SDL_Quit();
slouken@5254
    58
    exit(rc);
slouken@5254
    59
}
slouken@5254
    60
slouken@5255
    61
void UpdateTexture(SDL_Texture *texture, int frame)
slouken@5254
    62
{
slouken@5254
    63
    SDL_Color *color;
slouken@5254
    64
    Uint8 *src;
slouken@5254
    65
    Uint32 *dst;
slouken@5254
    66
    int row, col;
slouken@5254
    67
    void *pixels;
slouken@5254
    68
    int pitch;
slouken@5254
    69
slouken@5254
    70
    if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
slouken@5254
    71
        fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
slouken@5254
    72
        quit(5);
slouken@5254
    73
    }
slouken@5254
    74
    src = MooseFrames[frame];
slouken@5254
    75
    for (row = 0; row < MOOSEPIC_H; ++row) {
slouken@5254
    76
        dst = (Uint32*)((Uint8*)pixels + row * pitch);
slouken@5254
    77
        for (col = 0; col < MOOSEPIC_W; ++col) {
slouken@5254
    78
            color = &MooseColors[*src++];
slouken@5254
    79
            *dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
slouken@5254
    80
        }
slouken@5254
    81
    }
slouken@5254
    82
    SDL_UnlockTexture(texture);
slouken@5254
    83
}
slouken@5254
    84
slouken@5254
    85
int
slouken@5254
    86
main(int argc, char **argv)
slouken@5254
    87
{
slouken@5254
    88
    SDL_Window *window;
slouken@5254
    89
    SDL_Renderer *renderer;
slouken@5254
    90
    SDL_RWops *handle;
slouken@5254
    91
    SDL_Texture *MooseTexture;
slouken@5254
    92
    SDL_Event event;
slouken@5254
    93
    SDL_bool done = SDL_FALSE;
slouken@5254
    94
    int frame;
slouken@5254
    95
slouken@5254
    96
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@5254
    97
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@5254
    98
        return 1;
slouken@5254
    99
    }
slouken@5254
   100
slouken@5254
   101
    /* load the moose images */
slouken@5254
   102
    handle = SDL_RWFromFile("moose.dat", "rb");
slouken@5254
   103
    if (handle == NULL) {
slouken@5254
   104
        fprintf(stderr, "Can't find the file moose.dat !\n");
slouken@5254
   105
        quit(2);
slouken@5254
   106
    }
slouken@5254
   107
    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
slouken@5254
   108
    SDL_RWclose(handle);
slouken@5254
   109
slouken@5254
   110
slouken@5254
   111
    /* Create the window and renderer */
slouken@5254
   112
    window = SDL_CreateWindow("Happy Moose",
slouken@5254
   113
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   114
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   115
                              MOOSEPIC_W*4, MOOSEPIC_H*4,
slouken@5254
   116
                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
slouken@5254
   117
    if (!window) {
slouken@5254
   118
        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
slouken@5254
   119
        quit(3);
slouken@5254
   120
    }
slouken@5254
   121
slouken@5254
   122
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5254
   123
    if (!renderer) {
slouken@5254
   124
        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
slouken@5254
   125
        quit(4);
slouken@5254
   126
    }
slouken@5254
   127
slouken@5254
   128
    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
slouken@5254
   129
    if (!MooseTexture) {
slouken@5254
   130
        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
slouken@5254
   131
        quit(5);
slouken@5254
   132
    }
slouken@5254
   133
slouken@5254
   134
    /* Loop, waiting for QUIT or the escape key */
slouken@5254
   135
    frame = 0;
slouken@5254
   136
    while (!done) {
slouken@5254
   137
        while (SDL_PollEvent(&event)) {
slouken@5254
   138
            switch (event.type) {
slouken@5254
   139
            case SDL_KEYDOWN:
slouken@5254
   140
                if (event.key.keysym.sym == SDLK_ESCAPE) {
slouken@5254
   141
                    done = SDL_TRUE;
slouken@5254
   142
                }
slouken@5254
   143
                break;
slouken@5254
   144
            case SDL_QUIT:
slouken@5254
   145
                done = SDL_TRUE;
slouken@5254
   146
                break;
slouken@5254
   147
            }
slouken@5254
   148
        }
slouken@5254
   149
slouken@5254
   150
        frame = (frame + 1) % MOOSEFRAMES_COUNT;
slouken@5254
   151
        UpdateTexture(MooseTexture, frame);
slouken@5254
   152
slouken@5254
   153
        SDL_RenderClear(renderer);
slouken@5254
   154
        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
slouken@5254
   155
        SDL_RenderPresent(renderer);
slouken@5254
   156
    }
slouken@5254
   157
    SDL_DestroyRenderer(renderer);
slouken@5254
   158
slouken@5254
   159
    quit(0);
slouken@5254
   160
    return 0;
slouken@5254
   161
}
slouken@5254
   162
slouken@5254
   163
/* vi: set ts=4 sw=4 expandtab: */