SDL 2.0
testrendercopyex.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL_test_common.h"
+ Include dependency graph for testrendercopyex.c:

Go to the source code of this file.

Data Structures

struct  DrawState
 

Functions

static void quit (int rc)
 
SDL_TextureLoadTexture (SDL_Renderer *renderer, const char *file, SDL_bool transparent)
 
void Draw (DrawState *s)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

static SDLTest_CommonStatestate
 
DrawStatedrawstates
 
int done
 

Function Documentation

◆ Draw()

void Draw ( DrawState s)

Definition at line 98 of file testrendercopyex.c.

99{
102 SDL_Point *center=NULL;
103 SDL_Point origin = {0,0};
104
105 SDL_RenderGetViewport(s->renderer, &viewport);
106
108 SDL_SetRenderTarget(s->renderer, target);
109
110 /* Draw the background */
111 SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
112
113 /* Scale and draw the sprite */
114 s->sprite_rect.w += s->scale_direction;
115 s->sprite_rect.h += s->scale_direction;
116 if (s->scale_direction > 0) {
117 center = &origin;
118 if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
119 s->scale_direction = -1;
120 }
121 } else {
122 if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
123 s->scale_direction = 1;
124 }
125 }
126 s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
127 s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
128
129 SDL_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, (SDL_RendererFlip)s->scale_direction);
130
131 SDL_SetRenderTarget(s->renderer, NULL);
132 SDL_RenderCopy(s->renderer, target, NULL, NULL);
134
135 /* Update the screen! */
136 SDL_RenderPresent(s->renderer);
137 /* SDL_Delay(10); */
138}
#define SDL_RenderPresent
#define SDL_RenderGetViewport
#define SDL_DestroyTexture
#define SDL_CreateTexture
#define SDL_RenderCopyEx
#define SDL_SetRenderTarget
#define SDL_RenderCopy
GLdouble s
Definition: SDL_opengl.h:2063
GLenum target
@ SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:248
SDL_RendererFlip
Flip constants for SDL_RenderCopyEx.
Definition: SDL_render.h:112
@ SDL_TEXTUREACCESS_TARGET
Definition: SDL_render.h:95
#define NULL
Definition: begin_code.h:167
The structure that defines a point (integer)
Definition: SDL_rect.h:49
int x
Definition: SDL_rect.h:50
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
SDL_Rect viewport
Definition: testviewport.c:28

References SDL_Rect::h, NULL, SDL_CreateTexture, SDL_DestroyTexture, SDL_PIXELFORMAT_ARGB8888, SDL_RenderCopy, SDL_RenderCopyEx, SDL_RenderGetViewport, SDL_RenderPresent, SDL_SetRenderTarget, SDL_TEXTUREACCESS_TARGET, viewport, SDL_Rect::w, and SDL_Point::x.

Referenced by loop().

◆ LoadTexture()

SDL_Texture * LoadTexture ( SDL_Renderer renderer,
const char *  file,
SDL_bool  transparent 
)

Definition at line 48 of file testrendercopyex.c.

49{
50 SDL_Surface *temp;
52
53 /* Load the sprite image */
54 temp = SDL_LoadBMP(file);
55 if (temp == NULL) {
56 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
57 return NULL;
58 }
59
60 /* Set transparent pixel as the pixel at (0,0) */
61 if (transparent) {
62 if (temp->format->palette) {
63 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
64 } else {
65 switch (temp->format->BitsPerPixel) {
66 case 15:
68 (*(Uint16 *) temp->pixels) & 0x00007FFF);
69 break;
70 case 16:
71 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
72 break;
73 case 24:
75 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
76 break;
77 case 32:
78 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
79 break;
80 }
81 }
82 }
83
84 /* Create textures from the image */
86 if (!texture) {
87 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
88 SDL_FreeSurface(temp);
89 return NULL;
90 }
91 SDL_FreeSurface(temp);
92
93 /* We're ready to roll. :) */
94 return texture;
95}
#define SDL_SetColorKey
#define SDL_GetError
#define SDL_CreateTextureFromSurface
#define SDL_LogError
#define SDL_FreeSurface
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLenum GLenum GLuint texture
@ SDL_TRUE
Definition: SDL_stdinc.h:164
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint16_t Uint16
Definition: SDL_stdinc.h:191
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:201
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
SDL_Palette * palette
Definition: SDL_pixels.h:318
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:73
void * pixels
Definition: SDL_surface.h:76
static SDL_Renderer * renderer

References SDL_PixelFormat::BitsPerPixel, SDL_Surface::format, NULL, SDL_PixelFormat::palette, SDL_Surface::pixels, renderer, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_SetColorKey, and SDL_TRUE.

Referenced by main().

◆ loop()

void loop ( void  )

Definition at line 140 of file testrendercopyex.c.

141{
142 int i;
144
145 /* Check for events */
146
147 while (SDL_PollEvent(&event)) {
149 }
150 for (i = 0; i < state->num_windows; ++i) {
151 if (state->windows[i] == NULL)
152 continue;
153 Draw(&drawstates[i]);
154 }
155#ifdef __EMSCRIPTEN__
156 if (done) {
157 emscripten_cancel_main_loop();
158 }
159#endif
160}
#define SDL_PollEvent
struct _cl_event * event
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_Window ** windows
DrawState * drawstates
int done
static SDLTest_CommonState * state
void Draw(DrawState *s)
General event structure.
Definition: SDL_events.h:558

References done, Draw(), drawstates, i, NULL, SDLTest_CommonState::num_windows, SDL_PollEvent, SDLTest_CommonEvent(), state, and SDLTest_CommonState::windows.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 163 of file testrendercopyex.c.

164{
165 int i;
166 int frames;
167 Uint32 then, now;
168
169 /* Enable standard application logging */
171
172 /* Initialize test framework */
174 if (!state) {
175 return 1;
176 }
177
180 return 1;
181 }
182
184 for (i = 0; i < state->num_windows; ++i) {
185 DrawState *drawstate = &drawstates[i];
186
187 drawstate->window = state->windows[i];
188 drawstate->renderer = state->renderers[i];
189 drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
190 drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
191 if (!drawstate->sprite || !drawstate->background) {
192 quit(2);
193 }
194 SDL_QueryTexture(drawstate->sprite, NULL, NULL,
195 &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
196 drawstate->scale_direction = 1;
197 }
198
199 /* Main render loop */
200 frames = 0;
201 then = SDL_GetTicks();
202 done = 0;
203
204#ifdef __EMSCRIPTEN__
205 emscripten_set_main_loop(loop, 0, 1);
206#else
207 while (!done) {
208 ++frames;
209 loop();
210 }
211#endif
212 /* Print out some timing information */
213 now = SDL_GetTicks();
214 if (now > then) {
215 double fps = ((double) frames * 1000) / (now - then);
216 SDL_Log("%2.2f frames per second\n", fps);
217 }
218
220
221 quit(0);
222 return 0;
223}
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_QueryTexture
#define SDL_LogSetPriority
#define SDL_Log
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_FALSE
Definition: SDL_stdinc.h:163
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:354
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:355
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
SDL_bool SDLTest_CommonDefaultArgs(SDLTest_CommonState *state, const int argc, char **argv)
Easy argument handling when test app doesn't need any custom args.
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDL_Window * window
SDL_Texture * sprite
SDL_Texture * background
SDL_Renderer * renderer
SDL_Rect sprite_rect
SDL_Renderer ** renderers
static void quit(int rc)
SDL_Texture * LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
void loop()
static Uint32 frames
Definition: testsprite2.c:40

References DrawState::background, done, drawstates, frames, SDL_Rect::h, i, LoadTexture(), loop(), NULL, SDLTest_CommonState::num_windows, quit(), DrawState::renderer, SDLTest_CommonState::renderers, DrawState::scale_direction, SDL_FALSE, SDL_GetTicks(), SDL_INIT_VIDEO, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_QueryTexture, SDL_stack_alloc, SDL_stack_free, SDL_TRUE, SDLTest_CommonCreateState(), SDLTest_CommonDefaultArgs(), SDLTest_CommonInit(), SDLTest_CommonQuit(), DrawState::sprite, DrawState::sprite_rect, state, SDL_Rect::w, DrawState::window, and SDLTest_CommonState::windows.

◆ quit()

static void quit ( int  rc)
static

Definition at line 41 of file testrendercopyex.c.

42{
44 exit(rc);
45}

References SDLTest_CommonQuit(), and state.

Referenced by main().

Variable Documentation

◆ done

int done

Definition at line 37 of file testrendercopyex.c.

Referenced by loop(), and main().

◆ drawstates

DrawState* drawstates

Definition at line 36 of file testrendercopyex.c.

Referenced by loop(), and main().

◆ state

SDLTest_CommonState* state
static

Definition at line 25 of file testrendercopyex.c.

Referenced by loop(), main(), and quit().