teststreaming.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/********************************************************************************
* *
* Running moose :) Coded by Mike Gorchak. *
* *
********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "SDL.h"
#define MOOSEPIC_W 64
#define MOOSEPIC_H 88
#define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
#define MOOSEFRAMES_COUNT 10
SDL_Color MooseColors[84] = {
{49, 49, 49, 255}, {66, 24, 0, 255}, {66, 33, 0, 255}, {66, 66, 66, 255},
{66, 115, 49, 255}, {74, 33, 0, 255}, {74, 41, 16, 255}, {82, 33, 8, 255},
{82, 41, 8, 255}, {82, 49, 16, 255}, {82, 82, 82, 255}, {90, 41, 8, 255},
{90, 41, 16, 255}, {90, 57, 24, 255}, {99, 49, 16, 255}, {99, 66, 24, 255},
{99, 66, 33, 255}, {99, 74, 33, 255}, {107, 57, 24, 255}, {107, 82, 41, 255},
{115, 57, 33, 255}, {115, 66, 33, 255}, {115, 66, 41, 255}, {115, 74, 0, 255},
{115, 90, 49, 255}, {115, 115, 115, 255}, {123, 82, 0, 255}, {123, 99, 57, 255},
{132, 66, 41, 255}, {132, 74, 41, 255}, {132, 90, 8, 255}, {132, 99, 33, 255},
{132, 99, 66, 255}, {132, 107, 66, 255}, {140, 74, 49, 255}, {140, 99, 16, 255},
{140, 107, 74, 255}, {140, 115, 74, 255}, {148, 107, 24, 255}, {148, 115, 82, 255},
{148, 123, 74, 255}, {148, 123, 90, 255}, {156, 115, 33, 255}, {156, 115, 90, 255},
{156, 123, 82, 255}, {156, 132, 82, 255}, {156, 132, 99, 255}, {156, 156, 156, 255},
{165, 123, 49, 255}, {165, 123, 90, 255}, {165, 132, 82, 255}, {165, 132, 90, 255},
{165, 132, 99, 255}, {165, 140, 90, 255}, {173, 132, 57, 255}, {173, 132, 99, 255},
{173, 140, 107, 255}, {173, 140, 115, 255}, {173, 148, 99, 255}, {173, 173, 173, 255},
{181, 140, 74, 255}, {181, 148, 115, 255}, {181, 148, 123, 255}, {181, 156, 107, 255},
{189, 148, 123, 255}, {189, 156, 82, 255}, {189, 156, 123, 255}, {189, 156, 132, 255},
{189, 189, 189, 255}, {198, 156, 123, 255}, {198, 165, 132, 255}, {206, 165, 99, 255},
{206, 165, 132, 255}, {206, 173, 140, 255}, {206, 206, 206, 255}, {214, 173, 115, 255},
{214, 173, 140, 255}, {222, 181, 148, 255}, {222, 189, 132, 255}, {222, 189, 156, 255},
{222, 222, 222, 255}, {231, 198, 165, 255}, {231, 231, 231, 255}, {239, 206, 173, 255}
};
Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE];
SDL_Renderer *renderer;
int frame;
SDL_Texture *MooseTexture;
SDL_bool done = SDL_FALSE;
void quit(int rc)
{
SDL_Quit();
exit(rc);
}
void UpdateTexture(SDL_Texture *texture, int frame)
{
SDL_Color *color;
Uint8 *src;
Uint32 *dst;
int row, col;
void *pixels;
int pitch;
if (SDL_LockTexture(texture, NULL, &pixels, &pitch) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
quit(5);
}
src = MooseFrames[frame];
for (row = 0; row < MOOSEPIC_H; ++row) {
dst = (Uint32*)((Uint8*)pixels + row * pitch);
for (col = 0; col < MOOSEPIC_W; ++col) {
color = &MooseColors[*src++];
*dst++ = (0xFF000000|(color->r<<16)|(color->g<<8)|color->b);
}
}
SDL_UnlockTexture(texture);
}
void
loop()
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
done = SDL_TRUE;
}
break;
case SDL_QUIT:
done = SDL_TRUE;
break;
}
}
frame = (frame + 1) % MOOSEFRAMES_COUNT;
UpdateTexture(MooseTexture, frame);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, MooseTexture, NULL, NULL);
SDL_RenderPresent(renderer);
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int
main(int argc, char **argv)
{
SDL_Window *window;
SDL_RWops *handle;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
/* load the moose images */
handle = SDL_RWFromFile("moose.dat", "rb");
if (handle == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
quit(2);
}
SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
SDL_RWclose(handle);
/* Create the window and renderer */
window = SDL_CreateWindow("Happy Moose",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
MOOSEPIC_W*4, MOOSEPIC_H*4,
SDL_WINDOW_RESIZABLE);
if (!window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
quit(3);
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
quit(4);
}
MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
if (!MooseTexture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
quit(5);
}
/* Loop, waiting for QUIT or the escape key */
frame = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
loop();
}
#endif
SDL_DestroyRenderer(renderer);
quit(0);
return 0;
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:306
SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:248
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_KEYDOWN
Definition: SDL_events.h:93
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_Color
Definition: SDL_pixels.h:295
SDL_TRUE
Definition: SDL_stdinc.h:142
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:298
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
MOOSEFRAMES_COUNT
#define MOOSEFRAMES_COUNT
Definition: teststreaming.c:31
SDL_RWops
Definition: SDL_rwops.h:52
SDL_FALSE
Definition: SDL_stdinc.h:141
SDL_QUIT
Definition: SDL_events.h:60
SDL_Renderer
Definition: SDL_sysrender.h:76
SDL_RWread
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:299
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
SDL_WINDOWPOS_UNDEFINED
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:130
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
src
GLenum src
Definition: SDL_opengl_glext.h:1737
renderer
SDL_Renderer * renderer
Definition: teststreaming.c:59
SDL_Texture
Definition: SDL_sysrender.h:49
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDL_UnlockTexture
#define SDL_UnlockTexture
Definition: SDL_dynapi_overrides.h:318
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
texture
GLenum GLenum GLuint texture
Definition: SDL_opengl_glext.h:1178
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:297
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
SDL_LockTexture
#define SDL_LockTexture
Definition: SDL_dynapi_overrides.h:317
quit
void quit(int rc)
Definition: teststreaming.c:64
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
frame
int frame
Definition: teststreaming.c:60
loop
void loop()
Definition: teststreaming.c:95
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_TEXTUREACCESS_STREAMING
Definition: SDL_render.h:94
MooseTexture
SDL_Texture * MooseTexture
Definition: teststreaming.c:61
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:199
MOOSEPIC_W
#define MOOSEPIC_W
Definition: teststreaming.c:27
pixels
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
NULL
#define NULL
Definition: begin_code.h:164
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
SDL_WINDOW_RESIZABLE
Definition: SDL_video.h:105
SDL_RWclose
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:530
MooseColors
SDL_Color MooseColors[84]
Definition: teststreaming.c:33
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
MooseFrames
Uint8 MooseFrames[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE]
Definition: teststreaming.c:57
color
GLuint color
Definition: SDL_opengl_glext.h:1148
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
SDL_Init
#define SDL_Init
Definition: SDL_dynapi_overrides.h:54
SDL_Event
General event structure.
Definition: SDL_events.h:525
main
int main(int argc, char **argv)
Definition: teststreaming.c:127
SDL_DestroyRenderer
#define SDL_DestroyRenderer
Definition: SDL_dynapi_overrides.h:348
SDLK_ESCAPE
Definition: SDL_keycode.h:55
SDL.h
MOOSEFRAME_SIZE
#define MOOSEFRAME_SIZE
Definition: teststreaming.c:30
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
row
GLenum GLenum void * row
Definition: SDL_opengl_glext.h:3138
done
SDL_bool done
Definition: teststreaming.c:62
UpdateTexture
void UpdateTexture(SDL_Texture *texture, int frame)
Definition: teststreaming.c:70
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:78
SDL_CreateRenderer
#define SDL_CreateRenderer
Definition: SDL_dynapi_overrides.h:301
MOOSEPIC_H
#define MOOSEPIC_H
Definition: teststreaming.c:28
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_Event::type
Uint32 type
Definition: SDL_events.h:527