The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
This commit is contained in:
parent
52cf8a6451
commit
df94d4c6a4
15 changed files with 617 additions and 690 deletions
|
@ -646,7 +646,10 @@ CommonInit(CommonState * state)
|
|||
state->windows =
|
||||
(SDL_Window **) SDL_malloc(state->num_windows *
|
||||
sizeof(*state->windows));
|
||||
if (!state->windows) {
|
||||
state->renderers =
|
||||
(SDL_Renderer **) SDL_malloc(state->num_windows *
|
||||
sizeof(*state->renderers));
|
||||
if (!state->windows || !state->renderers) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
@ -685,6 +688,8 @@ CommonInit(CommonState * state)
|
|||
|
||||
SDL_ShowWindow(state->windows[i]);
|
||||
|
||||
state->renderers[i] = NULL;
|
||||
|
||||
if (!state->skip_renderer
|
||||
&& (state->renderdriver
|
||||
|| !(state->window_flags & SDL_WINDOW_OPENGL))) {
|
||||
|
@ -707,8 +712,9 @@ CommonInit(CommonState * state)
|
|||
return SDL_FALSE;
|
||||
}
|
||||
}
|
||||
if (SDL_CreateRenderer
|
||||
(state->windows[i], m, state->render_flags) < 0) {
|
||||
state->renderers[i] = SDL_CreateRenderer(state->windows[i],
|
||||
m, state->render_flags);
|
||||
if (!state->renderers[i]) {
|
||||
fprintf(stderr, "Couldn't create renderer: %s\n",
|
||||
SDL_GetError());
|
||||
return SDL_FALSE;
|
||||
|
@ -717,12 +723,11 @@ CommonInit(CommonState * state)
|
|||
SDL_RendererInfo info;
|
||||
|
||||
fprintf(stderr, "Current renderer:\n");
|
||||
SDL_GetRendererInfo(&info);
|
||||
SDL_GetRendererInfo(state->renderers[i], &info);
|
||||
PrintRenderer(&info);
|
||||
}
|
||||
}
|
||||
}
|
||||
SDL_SelectRenderer(state->windows[0]);
|
||||
}
|
||||
|
||||
if (state->flags & SDL_INIT_AUDIO) {
|
||||
|
@ -1012,15 +1017,25 @@ CommonEvent(CommonState * state, SDL_Event * event, int *done)
|
|||
void
|
||||
CommonQuit(CommonState * state)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (state->windows) {
|
||||
SDL_free(state->windows);
|
||||
}
|
||||
if (state->renderers) {
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
if (state->renderers[i]) {
|
||||
SDL_DestroyRenderer(state->renderers[i]);
|
||||
}
|
||||
}
|
||||
SDL_free(state->renderers);
|
||||
}
|
||||
if (state->flags & SDL_INIT_VIDEO) {
|
||||
SDL_VideoQuit();
|
||||
}
|
||||
if (state->flags & SDL_INIT_AUDIO) {
|
||||
SDL_AudioQuit();
|
||||
}
|
||||
if (state->windows) {
|
||||
SDL_free(state->windows);
|
||||
}
|
||||
SDL_free(state);
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ typedef struct
|
|||
const char *renderdriver;
|
||||
Uint32 render_flags;
|
||||
SDL_bool skip_renderer;
|
||||
SDL_Renderer **renderers;
|
||||
|
||||
/* Audio info */
|
||||
const char *audiodriver;
|
||||
|
|
|
@ -19,7 +19,7 @@ static int current_color = 255;
|
|||
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
|
||||
|
||||
void
|
||||
DrawPoints(SDL_Window * window)
|
||||
DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i;
|
||||
int x, y;
|
||||
|
@ -28,7 +28,6 @@ DrawPoints(SDL_Window * window)
|
|||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(blendMode);
|
||||
for (i = 0; i < num_objects * 4; ++i) {
|
||||
/* Cycle the color and alpha, if desired */
|
||||
if (cycle_color) {
|
||||
|
@ -53,18 +52,17 @@ DrawPoints(SDL_Window * window)
|
|||
cycle_direction = -cycle_direction;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
||||
SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
|
||||
(Uint8) current_color, (Uint8) current_alpha);
|
||||
|
||||
x = rand() % window_w;
|
||||
y = rand() % window_h;
|
||||
SDL_RenderDrawPoint(x, y);
|
||||
SDL_RenderDrawPoint(renderer, x, y);
|
||||
}
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
void
|
||||
DrawLines(SDL_Window * window)
|
||||
DrawLines(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i;
|
||||
int x1, y1, x2, y2;
|
||||
|
@ -73,7 +71,6 @@ DrawLines(SDL_Window * window)
|
|||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(blendMode);
|
||||
for (i = 0; i < num_objects; ++i) {
|
||||
/* Cycle the color and alpha, if desired */
|
||||
if (cycle_color) {
|
||||
|
@ -98,27 +95,26 @@ DrawLines(SDL_Window * window)
|
|||
cycle_direction = -cycle_direction;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
||||
SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
|
||||
(Uint8) current_color, (Uint8) current_alpha);
|
||||
|
||||
if (i == 0) {
|
||||
SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
|
||||
SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
|
||||
SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
|
||||
SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
|
||||
SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
|
||||
SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
|
||||
SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
|
||||
SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1);
|
||||
} else {
|
||||
x1 = (rand() % (window_w*2)) - window_w;
|
||||
x2 = (rand() % (window_w*2)) - window_w;
|
||||
y1 = (rand() % (window_h*2)) - window_h;
|
||||
y2 = (rand() % (window_h*2)) - window_h;
|
||||
SDL_RenderDrawLine(x1, y1, x2, y2);
|
||||
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
void
|
||||
DrawRects(SDL_Window * window)
|
||||
DrawRects(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i;
|
||||
SDL_Rect rect;
|
||||
|
@ -127,7 +123,6 @@ DrawRects(SDL_Window * window)
|
|||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(blendMode);
|
||||
for (i = 0; i < num_objects / 4; ++i) {
|
||||
/* Cycle the color and alpha, if desired */
|
||||
if (cycle_color) {
|
||||
|
@ -152,16 +147,15 @@ DrawRects(SDL_Window * window)
|
|||
cycle_direction = -cycle_direction;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
||||
SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
|
||||
(Uint8) current_color, (Uint8) current_alpha);
|
||||
|
||||
rect.w = rand() % (window_h / 2);
|
||||
rect.h = rand() % (window_h / 2);
|
||||
rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2);
|
||||
rect.y = (rand() % (window_h*2) - window_h) - (rect.h / 2);
|
||||
SDL_RenderFillRect(&rect);
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -223,9 +217,10 @@ main(int argc, char *argv[])
|
|||
|
||||
/* Create the windows and initialize the renderers */
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
SDL_SelectRenderer(state->windows[i]);
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_Renderer *renderer = state->renderers[i];
|
||||
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
}
|
||||
|
||||
srand((unsigned int)time(NULL));
|
||||
|
@ -239,30 +234,17 @@ main(int argc, char *argv[])
|
|||
++frames;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
CommonEvent(state, &event, &done);
|
||||
switch (event.type) {
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
SDL_SelectRenderer(state->windows[i]);
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_Renderer *renderer = state->renderers[i];
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
DrawRects(state->windows[i]);
|
||||
DrawLines(state->windows[i]);
|
||||
DrawPoints(state->windows[i]);
|
||||
DrawRects(state->windows[i], renderer);
|
||||
DrawLines(state->windows[i], renderer);
|
||||
DrawPoints(state->windows[i], renderer);
|
||||
|
||||
SDL_RenderPresent();
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ static int current_color = 255;
|
|||
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
|
||||
|
||||
void
|
||||
DrawPoints(SDL_Window * window)
|
||||
DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i;
|
||||
int x, y;
|
||||
|
@ -29,7 +29,6 @@ DrawPoints(SDL_Window * window)
|
|||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(blendMode);
|
||||
for (i = 0; i < num_objects * 4; ++i) {
|
||||
/* Cycle the color and alpha, if desired */
|
||||
if (cycle_color) {
|
||||
|
@ -54,14 +53,13 @@ DrawPoints(SDL_Window * window)
|
|||
cycle_direction = -cycle_direction;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(255, (Uint8) current_color,
|
||||
SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
|
||||
(Uint8) current_color, (Uint8) current_alpha);
|
||||
|
||||
x = rand() % window_w;
|
||||
y = rand() % window_h;
|
||||
SDL_RenderDrawPoint(x, y);
|
||||
SDL_RenderDrawPoint(renderer, x, y);
|
||||
}
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
#define MAX_LINES 16
|
||||
|
@ -86,7 +84,7 @@ add_line(int x1, int y1, int x2, int y2)
|
|||
|
||||
|
||||
void
|
||||
DrawLines(SDL_Window * window)
|
||||
DrawLines(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i;
|
||||
int x1, y1, x2, y2;
|
||||
|
@ -95,20 +93,18 @@ DrawLines(SDL_Window * window)
|
|||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(blendMode);
|
||||
for (i = 0; i < num_lines; ++i) {
|
||||
SDL_SetRenderDrawColor(255, 255, 255, 255);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
|
||||
if (i == -1) {
|
||||
SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
|
||||
SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
|
||||
SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
|
||||
SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
|
||||
SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
|
||||
SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
|
||||
SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
|
||||
SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1);
|
||||
} else {
|
||||
SDL_RenderDrawLine(lines[i].x, lines[i].y, lines[i].w, lines[i].h);
|
||||
SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
#define MAX_RECTS 16
|
||||
|
@ -139,7 +135,7 @@ add_rect(int x1, int y1, int x2, int y2)
|
|||
}
|
||||
|
||||
static void
|
||||
DrawRects(SDL_Window * window)
|
||||
DrawRects(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i;
|
||||
int window_w, window_h;
|
||||
|
@ -147,24 +143,20 @@ DrawRects(SDL_Window * window)
|
|||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
for (i = 0; i < num_rects; ++i) {
|
||||
SDL_SetRenderDrawColor(255, 127, 0, 255);
|
||||
SDL_RenderFillRect(&rects[i]);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
|
||||
SDL_RenderFillRect(renderer, &rects[i]);
|
||||
}
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
DrawRectLineIntersections(SDL_Window * window)
|
||||
DrawRectLineIntersections(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i, j, window_w, window_h;
|
||||
|
||||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
|
||||
for (i = 0; i < num_rects; i++)
|
||||
for (j = 0; j < num_lines; j++) {
|
||||
int x1, y1, x2, y2;
|
||||
|
@ -177,31 +169,25 @@ DrawRectLineIntersections(SDL_Window * window)
|
|||
y2 = lines[j].h;
|
||||
|
||||
if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
|
||||
SDL_SetRenderDrawColor(0, 255, 55, 255);
|
||||
SDL_RenderDrawLine(x1, y1, x2, y2);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
|
||||
SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
DrawRectRectIntersections(SDL_Window * window)
|
||||
DrawRectRectIntersections(SDL_Window * window, SDL_Renderer * renderer)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
|
||||
for (i = 0; i < num_rects; i++)
|
||||
for (j = i + 1; j < num_rects; j++) {
|
||||
SDL_Rect r;
|
||||
if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
|
||||
SDL_SetRenderDrawColor(255, 200, 0, 255);
|
||||
SDL_RenderFillRect(&r);
|
||||
SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
|
||||
SDL_RenderFillRect(renderer, &r);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -264,9 +250,10 @@ main(int argc, char *argv[])
|
|||
|
||||
/* Create the windows and initialize the renderers */
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
SDL_SelectRenderer(state->windows[i]);
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_Renderer *renderer = state->renderers[i];
|
||||
SDL_SetRenderDrawBlendMode(renderer, blendMode);
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
}
|
||||
|
||||
srand(time(NULL));
|
||||
|
@ -311,31 +298,22 @@ main(int argc, char *argv[])
|
|||
break;
|
||||
}
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
SDL_SelectRenderer(state->windows[i]);
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_Renderer *renderer = state->renderers[i];
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
DrawRects(state->windows[i]);
|
||||
DrawPoints(state->windows[i]);
|
||||
DrawRectRectIntersections(state->windows[i]);
|
||||
DrawLines(state->windows[i]);
|
||||
DrawRectLineIntersections(state->windows[i]);
|
||||
DrawRects(state->windows[i], renderer);
|
||||
DrawPoints(state->windows[i], renderer);
|
||||
DrawRectRectIntersections(state->windows[i], renderer);
|
||||
DrawLines(state->windows[i], renderer);
|
||||
DrawRectLineIntersections(state->windows[i], renderer);
|
||||
|
||||
SDL_RenderPresent();
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
320
test/testshape.c
320
test/testshape.c
|
@ -11,177 +11,183 @@
|
|||
#define TICK_INTERVAL 1000/10
|
||||
|
||||
typedef struct LoadedPicture {
|
||||
SDL_Surface *surface;
|
||||
SDL_Texture *texture;
|
||||
SDL_WindowShapeMode mode;
|
||||
SDL_Surface *surface;
|
||||
SDL_Texture *texture;
|
||||
SDL_WindowShapeMode mode;
|
||||
} LoadedPicture;
|
||||
|
||||
void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
|
||||
SDL_SelectRenderer(window);
|
||||
|
||||
//Clear render-target to blue.
|
||||
SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
|
||||
SDL_RenderClear();
|
||||
|
||||
//Render the texture.
|
||||
SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
|
||||
|
||||
SDL_RenderPresent();
|
||||
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
|
||||
{
|
||||
//Clear render-target to blue.
|
||||
SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
//Render the texture.
|
||||
SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
static Uint32 next_time;
|
||||
|
||||
Uint32 time_left() {
|
||||
Uint32 time_left()
|
||||
{
|
||||
Uint32 now = SDL_GetTicks();
|
||||
if(next_time <= now)
|
||||
return 0;
|
||||
else
|
||||
else
|
||||
return next_time - now;
|
||||
}
|
||||
|
||||
int main(int argc,char** argv) {
|
||||
Uint8 num_pictures;
|
||||
LoadedPicture* pictures;
|
||||
int i, j;
|
||||
SDL_PixelFormat* format = NULL;
|
||||
SDL_Window *window;
|
||||
SDL_Color black = {0,0,0,0xff};
|
||||
SDL_Event event;
|
||||
int event_pending = 0;
|
||||
int should_exit = 0;
|
||||
unsigned int current_picture;
|
||||
int button_down;
|
||||
Uint32 pixelFormat = 0;
|
||||
int access = 0;
|
||||
SDL_Rect texture_dimensions;;
|
||||
int main(int argc,char** argv)
|
||||
{
|
||||
Uint8 num_pictures;
|
||||
LoadedPicture* pictures;
|
||||
int i, j;
|
||||
SDL_PixelFormat* format = NULL;
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Color black = {0,0,0,0xff};
|
||||
SDL_Event event;
|
||||
int event_pending = 0;
|
||||
int should_exit = 0;
|
||||
unsigned int current_picture;
|
||||
int button_down;
|
||||
Uint32 pixelFormat = 0;
|
||||
int access = 0;
|
||||
SDL_Rect texture_dimensions;;
|
||||
|
||||
if(argc < 2) {
|
||||
printf("SDL_Shape requires at least one bitmap file as argument.\n");
|
||||
exit(-1);
|
||||
if(argc < 2) {
|
||||
printf("SDL_Shape requires at least one bitmap file as argument.\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if(SDL_VideoInit(NULL) == -1) {
|
||||
printf("Could not initialize SDL video.\n");
|
||||
exit(-2);
|
||||
}
|
||||
|
||||
num_pictures = argc - 1;
|
||||
pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
|
||||
for(i=0;i<num_pictures;i++)
|
||||
pictures[i].surface = NULL;
|
||||
for(i=0;i<num_pictures;i++) {
|
||||
pictures[i].surface = SDL_LoadBMP(argv[i+1]);
|
||||
if(pictures[i].surface == NULL) {
|
||||
j = 0;
|
||||
for(j=0;j<num_pictures;j++)
|
||||
if(pictures[j].surface != NULL)
|
||||
SDL_FreeSurface(pictures[j].surface);
|
||||
free(pictures);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not load surface from named bitmap file.\n");
|
||||
exit(-3);
|
||||
}
|
||||
|
||||
if(SDL_VideoInit(NULL) == -1) {
|
||||
printf("Could not initialize SDL video.\n");
|
||||
exit(-2);
|
||||
}
|
||||
|
||||
num_pictures = argc - 1;
|
||||
pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
|
||||
for(i=0;i<num_pictures;i++)
|
||||
pictures[i].surface = NULL;
|
||||
for(i=0;i<num_pictures;i++) {
|
||||
pictures[i].surface = SDL_LoadBMP(argv[i+1]);
|
||||
if(pictures[i].surface == NULL) {
|
||||
j = 0;
|
||||
for(j=0;j<num_pictures;j++)
|
||||
if(pictures[j].surface != NULL)
|
||||
SDL_FreeSurface(pictures[j].surface);
|
||||
free(pictures);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not load surface from named bitmap file.\n");
|
||||
exit(-3);
|
||||
}
|
||||
|
||||
format = pictures[i].surface->format;
|
||||
if(format->Amask != 0) {
|
||||
pictures[i].mode.mode = ShapeModeBinarizeAlpha;
|
||||
pictures[i].mode.parameters.binarizationCutoff = 255;
|
||||
}
|
||||
else {
|
||||
pictures[i].mode.mode = ShapeModeColorKey;
|
||||
pictures[i].mode.parameters.colorKey = black;
|
||||
}
|
||||
}
|
||||
|
||||
window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
|
||||
if(window == NULL) {
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not create shaped window for SDL_Shape.\n");
|
||||
exit(-4);
|
||||
}
|
||||
if(SDL_CreateRenderer(window,-1,0) == -1) {
|
||||
SDL_DestroyWindow(window);
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not create rendering context for SDL_Shape window.\n");
|
||||
exit(-5);
|
||||
}
|
||||
|
||||
for(i=0;i<num_pictures;i++)
|
||||
pictures[i].texture = NULL;
|
||||
for(i=0;i<num_pictures;i++) {
|
||||
pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
|
||||
if(pictures[i].texture == NULL) {
|
||||
j = 0;
|
||||
for(j=0;j<num_pictures;i++)
|
||||
if(pictures[i].texture != NULL)
|
||||
SDL_DestroyTexture(pictures[i].texture);
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
SDL_DestroyRenderer(window);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not create texture for SDL_shape.\n");
|
||||
exit(-6);
|
||||
}
|
||||
}
|
||||
|
||||
event_pending = 0;
|
||||
should_exit = 0;
|
||||
event_pending = SDL_PollEvent(&event);
|
||||
current_picture = 0;
|
||||
button_down = 0;
|
||||
texture_dimensions.h = 0;
|
||||
texture_dimensions.w = 0;
|
||||
texture_dimensions.x = 0;
|
||||
texture_dimensions.y = 0;
|
||||
SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
|
||||
SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
|
||||
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
|
||||
next_time = SDL_GetTicks() + TICK_INTERVAL;
|
||||
while(should_exit == 0) {
|
||||
event_pending = SDL_PollEvent(&event);
|
||||
if(event_pending == 1) {
|
||||
if(event.type == SDL_KEYDOWN) {
|
||||
button_down = 1;
|
||||
if(event.key.keysym.sym == SDLK_ESCAPE)
|
||||
should_exit = 1;
|
||||
}
|
||||
if(button_down && event.type == SDL_KEYUP) {
|
||||
button_down = 0;
|
||||
current_picture += 1;
|
||||
if(current_picture >= num_pictures)
|
||||
current_picture = 0;
|
||||
SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
|
||||
SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
|
||||
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
|
||||
}
|
||||
if(event.type == SDL_QUIT)
|
||||
should_exit = 1;
|
||||
event_pending = 0;
|
||||
}
|
||||
render(window,pictures[current_picture].texture,texture_dimensions);
|
||||
SDL_Delay(time_left());
|
||||
next_time += TICK_INTERVAL;
|
||||
}
|
||||
|
||||
//Free the textures.
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_DestroyTexture(pictures[i].texture);
|
||||
//Destroy the window.
|
||||
SDL_DestroyWindow(window);
|
||||
//Free the original surfaces backing the textures.
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
//Call SDL_VideoQuit() before quitting.
|
||||
SDL_VideoQuit();
|
||||
format = pictures[i].surface->format;
|
||||
if(format->Amask != 0) {
|
||||
pictures[i].mode.mode = ShapeModeBinarizeAlpha;
|
||||
pictures[i].mode.parameters.binarizationCutoff = 255;
|
||||
}
|
||||
else {
|
||||
pictures[i].mode.mode = ShapeModeColorKey;
|
||||
pictures[i].mode.parameters.colorKey = black;
|
||||
}
|
||||
}
|
||||
|
||||
window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
|
||||
if(window == NULL) {
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not create shaped window for SDL_Shape.\n");
|
||||
exit(-4);
|
||||
}
|
||||
renderer = SDL_CreateRenderer(window,-1,0);
|
||||
if (!renderer) {
|
||||
SDL_DestroyWindow(window);
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not create rendering context for SDL_Shape window.\n");
|
||||
exit(-5);
|
||||
}
|
||||
|
||||
for(i=0;i<num_pictures;i++)
|
||||
pictures[i].texture = NULL;
|
||||
for(i=0;i<num_pictures;i++) {
|
||||
pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface);
|
||||
if(pictures[i].texture == NULL) {
|
||||
j = 0;
|
||||
for(j=0;j<num_pictures;i++)
|
||||
if(pictures[i].texture != NULL)
|
||||
SDL_DestroyTexture(pictures[i].texture);
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_VideoQuit();
|
||||
printf("Could not create texture for SDL_shape.\n");
|
||||
exit(-6);
|
||||
}
|
||||
}
|
||||
|
||||
event_pending = 0;
|
||||
should_exit = 0;
|
||||
event_pending = SDL_PollEvent(&event);
|
||||
current_picture = 0;
|
||||
button_down = 0;
|
||||
texture_dimensions.h = 0;
|
||||
texture_dimensions.w = 0;
|
||||
texture_dimensions.x = 0;
|
||||
texture_dimensions.y = 0;
|
||||
SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
|
||||
SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
|
||||
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
|
||||
next_time = SDL_GetTicks() + TICK_INTERVAL;
|
||||
while(should_exit == 0) {
|
||||
event_pending = SDL_PollEvent(&event);
|
||||
if(event_pending == 1) {
|
||||
if(event.type == SDL_KEYDOWN) {
|
||||
button_down = 1;
|
||||
if(event.key.keysym.sym == SDLK_ESCAPE)
|
||||
should_exit = 1;
|
||||
}
|
||||
if(button_down && event.type == SDL_KEYUP) {
|
||||
button_down = 0;
|
||||
current_picture += 1;
|
||||
if(current_picture >= num_pictures)
|
||||
current_picture = 0;
|
||||
SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
|
||||
SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
|
||||
SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
|
||||
}
|
||||
if(event.type == SDL_QUIT)
|
||||
should_exit = 1;
|
||||
event_pending = 0;
|
||||
}
|
||||
render(renderer,pictures[current_picture].texture,texture_dimensions);
|
||||
SDL_Delay(time_left());
|
||||
next_time += TICK_INTERVAL;
|
||||
}
|
||||
|
||||
//Free the textures.
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_DestroyTexture(pictures[i].texture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
//Destroy the window.
|
||||
SDL_DestroyWindow(window);
|
||||
//Free the original surfaces backing the textures.
|
||||
for(i=0;i<num_pictures;i++)
|
||||
SDL_FreeSurface(pictures[i].surface);
|
||||
free(pictures);
|
||||
//Call SDL_VideoQuit() before quitting.
|
||||
SDL_VideoQuit();
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
|
||||
#include "common.h"
|
||||
|
||||
#define NUM_SPRITES 100
|
||||
#define MAX_SPEED 1
|
||||
#define NUM_SPRITES 100
|
||||
#define MAX_SPEED 1
|
||||
|
||||
static CommonState *state;
|
||||
static int num_sprites;
|
||||
|
@ -76,12 +76,12 @@ LoadSprite(char *file)
|
|||
|
||||
/* Create textures from the image */
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
SDL_SelectRenderer(state->windows[i]);
|
||||
sprites[i] = SDL_CreateTextureFromSurface(0, temp);
|
||||
SDL_Renderer *renderer = state->renderers[i];
|
||||
sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
|
||||
if (!sprites[i]) {
|
||||
SDL_SetColorKey(temp, 0, 0);
|
||||
sprites[i] = SDL_CreateTextureFromSurface(0, temp);
|
||||
}
|
||||
SDL_SetColorKey(temp, 0, 0);
|
||||
sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
|
||||
}
|
||||
if (!sprites[i]) {
|
||||
fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
|
||||
SDL_FreeSurface(temp);
|
||||
|
@ -96,15 +96,13 @@ LoadSprite(char *file)
|
|||
}
|
||||
|
||||
void
|
||||
MoveSprites(SDL_Window * window, SDL_Texture * sprite)
|
||||
MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
|
||||
{
|
||||
int i, n;
|
||||
int window_w, window_h;
|
||||
SDL_Rect temp;
|
||||
SDL_Rect *position, *velocity;
|
||||
|
||||
SDL_SelectRenderer(window);
|
||||
|
||||
/* Query the sizes */
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
|
||||
|
@ -136,55 +134,55 @@ MoveSprites(SDL_Window * window, SDL_Texture * sprite)
|
|||
}
|
||||
|
||||
/* Draw a gray background */
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
/* Test points */
|
||||
SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
|
||||
SDL_RenderDrawPoint(0, 0);
|
||||
SDL_RenderDrawPoint(window_w-1, 0);
|
||||
SDL_RenderDrawPoint(0, window_h-1);
|
||||
SDL_RenderDrawPoint(window_w-1, window_h-1);
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
|
||||
SDL_RenderDrawPoint(renderer, 0, 0);
|
||||
SDL_RenderDrawPoint(renderer, window_w-1, 0);
|
||||
SDL_RenderDrawPoint(renderer, 0, window_h-1);
|
||||
SDL_RenderDrawPoint(renderer, window_w-1, window_h-1);
|
||||
|
||||
/* Test horizontal and vertical lines */
|
||||
SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
|
||||
SDL_RenderDrawLine(1, 0, window_w-2, 0);
|
||||
SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1);
|
||||
SDL_RenderDrawLine(0, 1, 0, window_h-2);
|
||||
SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2);
|
||||
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
|
||||
SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0);
|
||||
SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1);
|
||||
SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2);
|
||||
SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2);
|
||||
|
||||
/* Test fill and copy */
|
||||
SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
temp.x = 1;
|
||||
temp.y = 1;
|
||||
temp.w = sprite_w;
|
||||
temp.h = sprite_h;
|
||||
SDL_RenderFillRect(&temp);
|
||||
SDL_RenderCopy(sprite, NULL, &temp);
|
||||
SDL_RenderFillRect(renderer, &temp);
|
||||
SDL_RenderCopy(renderer, sprite, NULL, &temp);
|
||||
temp.x = window_w-sprite_w-1;
|
||||
temp.y = 1;
|
||||
temp.w = sprite_w;
|
||||
temp.h = sprite_h;
|
||||
SDL_RenderFillRect(&temp);
|
||||
SDL_RenderCopy(sprite, NULL, &temp);
|
||||
SDL_RenderFillRect(renderer, &temp);
|
||||
SDL_RenderCopy(renderer, sprite, NULL, &temp);
|
||||
temp.x = 1;
|
||||
temp.y = window_h-sprite_h-1;
|
||||
temp.w = sprite_w;
|
||||
temp.h = sprite_h;
|
||||
SDL_RenderFillRect(&temp);
|
||||
SDL_RenderCopy(sprite, NULL, &temp);
|
||||
SDL_RenderFillRect(renderer, &temp);
|
||||
SDL_RenderCopy(renderer, sprite, NULL, &temp);
|
||||
temp.x = window_w-sprite_w-1;
|
||||
temp.y = window_h-sprite_h-1;
|
||||
temp.w = sprite_w;
|
||||
temp.h = sprite_h;
|
||||
SDL_RenderFillRect(&temp);
|
||||
SDL_RenderCopy(sprite, NULL, &temp);
|
||||
SDL_RenderFillRect(renderer, &temp);
|
||||
SDL_RenderCopy(renderer, sprite, NULL, &temp);
|
||||
|
||||
/* Test diagonal lines */
|
||||
SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
|
||||
SDL_RenderDrawLine(sprite_w, sprite_h,
|
||||
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
|
||||
SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
|
||||
window_w-sprite_w-2, window_h-sprite_h-2);
|
||||
SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h,
|
||||
SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h,
|
||||
sprite_w, window_h-sprite_h-2);
|
||||
|
||||
/* Move the sprite, bounce at the wall, and draw */
|
||||
|
@ -204,11 +202,11 @@ MoveSprites(SDL_Window * window, SDL_Texture * sprite)
|
|||
}
|
||||
|
||||
/* Blit the sprite onto the screen */
|
||||
SDL_RenderCopy(sprite, NULL, position);
|
||||
SDL_RenderCopy(renderer, sprite, NULL, position);
|
||||
}
|
||||
|
||||
/* Update the screen! */
|
||||
SDL_RenderPresent();
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -276,9 +274,9 @@ main(int argc, char *argv[])
|
|||
quit(2);
|
||||
}
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
SDL_SelectRenderer(state->windows[i]);
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_Renderer *renderer = state->renderers[i];
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
}
|
||||
if (LoadSprite("icon.bmp") < 0) {
|
||||
quit(2);
|
||||
|
@ -314,22 +312,9 @@ main(int argc, char *argv[])
|
|||
++frames;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
CommonEvent(state, &event, &done);
|
||||
switch (event.type) {
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < state->num_windows; ++i) {
|
||||
MoveSprites(state->windows[i], sprites[i]);
|
||||
MoveSprites(state->windows[i], state->renderers[i], sprites[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -340,7 +325,7 @@ main(int argc, char *argv[])
|
|||
printf("%2.2f frames per second\n", fps);
|
||||
}
|
||||
quit(0);
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
|
|
@ -24,7 +24,7 @@ quit(int rc)
|
|||
}
|
||||
|
||||
int
|
||||
LoadSprite(char *file)
|
||||
LoadSprite(char *file, SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_Surface *temp;
|
||||
|
||||
|
@ -60,10 +60,10 @@ LoadSprite(char *file)
|
|||
}
|
||||
|
||||
/* Create textures from the image */
|
||||
sprite = SDL_CreateTextureFromSurface(0, temp);
|
||||
sprite = SDL_CreateTextureFromSurface(renderer, 0, temp);
|
||||
if (!sprite) {
|
||||
SDL_SetColorKey(temp, 0, 0);
|
||||
sprite = SDL_CreateTextureFromSurface(0, temp);
|
||||
sprite = SDL_CreateTextureFromSurface(renderer, 0, temp);
|
||||
}
|
||||
if (!sprite) {
|
||||
fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
|
||||
|
@ -77,7 +77,7 @@ LoadSprite(char *file)
|
|||
}
|
||||
|
||||
void
|
||||
MoveSprites(SDL_Window * window, SDL_Texture * sprite)
|
||||
MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
|
||||
{
|
||||
int i;
|
||||
int window_w = WINDOW_WIDTH;
|
||||
|
@ -85,8 +85,8 @@ MoveSprites(SDL_Window * window, SDL_Texture * sprite)
|
|||
SDL_Rect *position, *velocity;
|
||||
|
||||
/* Draw a gray background */
|
||||
SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear();
|
||||
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
/* Move the sprite, bounce at the wall, and draw */
|
||||
for (i = 0; i < NUM_SPRITES; ++i) {
|
||||
|
@ -104,17 +104,18 @@ MoveSprites(SDL_Window * window, SDL_Texture * sprite)
|
|||
}
|
||||
|
||||
/* Blit the sprite onto the screen */
|
||||
SDL_RenderCopy(sprite, NULL, position);
|
||||
SDL_RenderCopy(renderer, sprite, NULL, position);
|
||||
}
|
||||
|
||||
/* Update the screen! */
|
||||
SDL_RenderPresent();
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
int i, done;
|
||||
SDL_Event event;
|
||||
|
||||
|
@ -127,7 +128,12 @@ main(int argc, char *argv[])
|
|||
quit(2);
|
||||
}
|
||||
|
||||
if (LoadSprite("icon.bmp") < 0) {
|
||||
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||
if (!renderer) {
|
||||
quit(2);
|
||||
}
|
||||
|
||||
if (LoadSprite("icon.bmp", renderer) < 0) {
|
||||
quit(2);
|
||||
}
|
||||
|
||||
|
@ -155,7 +161,7 @@ main(int argc, char *argv[])
|
|||
done = 1;
|
||||
}
|
||||
}
|
||||
MoveSprites(window, sprite);
|
||||
MoveSprites(window, renderer, sprite);
|
||||
}
|
||||
|
||||
quit(0);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue