-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testdrawchessboard: Allow using the standard render API
- Loading branch information
1 parent
da4608e
commit c99670a
Showing
1 changed file
with
22 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,10 @@ | |
This file is created by : Nitin Jain ([email protected]) | ||
*/ | ||
|
||
/* Sample program: Draw a Chess Board by using SDL_CreateSoftwareRenderer API */ | ||
/* Sample program: Draw a Chess Board by using the SDL render API */ | ||
|
||
/* This allows testing SDL_CreateSoftwareRenderer with the window surface API. Undefine it to use the accelerated renderer instead. */ | ||
#define USE_SOFTWARE_RENDERER | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
@@ -25,9 +28,12 @@ | |
|
||
SDL_Window *window; | ||
SDL_Renderer *renderer; | ||
SDL_Surface *surface; | ||
int done; | ||
|
||
#ifdef USE_SOFTWARE_RENDERER | ||
SDL_Surface *surface; | ||
#endif | ||
|
||
void DrawChessBoard(void) | ||
{ | ||
int row = 0, column = 0, x = 0; | ||
|
@@ -50,14 +56,14 @@ void DrawChessBoard(void) | |
SDL_RenderFillRect(renderer, &rect); | ||
} | ||
} | ||
SDL_RenderPresent(renderer); | ||
} | ||
|
||
void loop(void) | ||
{ | ||
SDL_Event e; | ||
while (SDL_PollEvent(&e)) { | ||
|
||
#ifdef USE_SOFTWARE_RENDERER | ||
/* Re-create when window has been resized */ | ||
if ((e.type == SDL_WINDOWEVENT) && (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)) { | ||
|
||
|
@@ -69,6 +75,7 @@ void loop(void) | |
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); | ||
SDL_RenderClear(renderer); | ||
} | ||
#endif | ||
|
||
if (e.type == SDL_QUIT) { | ||
done = 1; | ||
|
@@ -87,11 +94,19 @@ void loop(void) | |
} | ||
} | ||
|
||
/* Clear the rendering surface with the specified color */ | ||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); | ||
SDL_RenderClear(renderer); | ||
|
||
DrawChessBoard(); | ||
|
||
SDL_RenderPresent(renderer); | ||
|
||
#ifdef USE_SOFTWARE_RENDERER | ||
/* Got everything on rendering surface, | ||
now Update the drawing image on window screen */ | ||
SDL_UpdateWindowSurface(window); | ||
#endif | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
|
@@ -111,17 +126,17 @@ int main(int argc, char *argv[]) | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Window creation fail : %s\n", SDL_GetError()); | ||
return 1; | ||
} | ||
#ifdef USE_SOFTWARE_RENDERER | ||
surface = SDL_GetWindowSurface(window); | ||
renderer = SDL_CreateSoftwareRenderer(surface); | ||
#else | ||
renderer = SDL_CreateRenderer(window, -1, 0); | ||
#endif | ||
if (!renderer) { | ||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Render creation for surface fail : %s\n", SDL_GetError()); | ||
return 1; | ||
} | ||
|
||
/* Clear the rendering surface with the specified color */ | ||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); | ||
SDL_RenderClear(renderer); | ||
|
||
/* Draw the Image on rendering surface */ | ||
done = 0; | ||
#ifdef __EMSCRIPTEN__ | ||
|