test/teststreaming.c
author Edward Rudd <urkle@outoforder.cc>
Sat, 12 Oct 2013 11:21:40 -0400
changeset 7807 73da2747c268
parent 7639 9406b7dd2f2d
child 8149 681eb46b8ac4
permissions -rw-r--r--
added simple Drag & drop test
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 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) {
aschiffler@7639
    71
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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
aschiffler@7639
    96
	/* Enable standard application logging */
aschiffler@7639
    97
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    98
slouken@5254
    99
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
aschiffler@7639
   100
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@5254
   101
        return 1;
slouken@5254
   102
    }
slouken@5254
   103
slouken@5254
   104
    /* load the moose images */
slouken@5254
   105
    handle = SDL_RWFromFile("moose.dat", "rb");
slouken@5254
   106
    if (handle == NULL) {
aschiffler@7639
   107
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
slouken@5254
   108
        quit(2);
slouken@5254
   109
    }
slouken@5254
   110
    SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
slouken@5254
   111
    SDL_RWclose(handle);
slouken@5254
   112
slouken@5254
   113
slouken@5254
   114
    /* Create the window and renderer */
slouken@5254
   115
    window = SDL_CreateWindow("Happy Moose",
slouken@5254
   116
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   117
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5254
   118
                              MOOSEPIC_W*4, MOOSEPIC_H*4,
philipp@7478
   119
                              SDL_WINDOW_RESIZABLE);
slouken@5254
   120
    if (!window) {
aschiffler@7639
   121
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
slouken@5254
   122
        quit(3);
slouken@5254
   123
    }
slouken@5254
   124
slouken@5254
   125
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5254
   126
    if (!renderer) {
aschiffler@7639
   127
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
slouken@5254
   128
        quit(4);
slouken@5254
   129
    }
slouken@5254
   130
slouken@5254
   131
    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
slouken@5254
   132
    if (!MooseTexture) {
aschiffler@7639
   133
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
slouken@5254
   134
        quit(5);
slouken@5254
   135
    }
slouken@5254
   136
slouken@5254
   137
    /* Loop, waiting for QUIT or the escape key */
slouken@5254
   138
    frame = 0;
slouken@5254
   139
    while (!done) {
slouken@5254
   140
        while (SDL_PollEvent(&event)) {
slouken@5254
   141
            switch (event.type) {
slouken@5254
   142
            case SDL_KEYDOWN:
slouken@5254
   143
                if (event.key.keysym.sym == SDLK_ESCAPE) {
slouken@5254
   144
                    done = SDL_TRUE;
slouken@5254
   145
                }
slouken@5254
   146
                break;
slouken@5254
   147
            case SDL_QUIT:
slouken@5254
   148
                done = SDL_TRUE;
slouken@5254
   149
                break;
slouken@5254
   150
            }
slouken@5254
   151
        }
slouken@5254
   152
slouken@5254
   153
        frame = (frame + 1) % MOOSEFRAMES_COUNT;
slouken@5254
   154
        UpdateTexture(MooseTexture, frame);
slouken@5254
   155
slouken@5254
   156
        SDL_RenderClear(renderer);
slouken@5254
   157
        SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
slouken@5254
   158
        SDL_RenderPresent(renderer);
slouken@5254
   159
    }
slouken@5254
   160
    SDL_DestroyRenderer(renderer);
slouken@5254
   161
slouken@5254
   162
    quit(0);
slouken@5254
   163
    return 0;
slouken@5254
   164
}
slouken@5254
   165
slouken@5254
   166
/* vi: set ts=4 sw=4 expandtab: */