Reputation: 69
I'm trying to draw some graphic primitives on my screen using SDL2 with the SDL2_gfx library. But somehow the SDL2_gfx functions seem not to work. The following code is supposed to draw a simple green filled rectangle on white background
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define SCREENW 1366
#define SCREENH 768
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
bool init_SDL() {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n",SDL_GetError());
return false;
}
window = SDL_CreateWindow("SDL_gfx_Test",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! SDL Error: %s\n",SDL_GetError());
return false;
}
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
if(renderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n",SDL_GetError());
return false;
}
if(SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF) < 0) {
printf("Renderer color could not be set! SDL Error: %s\n",SDL_GetError());
return false;
}
return true;
}
void close_SDL() {
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc,char *argv[]) {
Sint16 topRightX = 100;
Sint16 topRightY = 100;
Sint16 bottomLeftX = 300;
Sint16 bottomLeftY = 300;
Uint32 green = 0x00FF00FF;
if(!init_SDL()) {
exit(EXIT_FAILURE);
}
SDL_RenderClear(renderer);
if(boxColor(renderer,topRightX,topRightY,bottomLeftX,bottomLeftY,green) < 0) {
printf("Could not draw box to renderer!\n");
exit(EXIT_FAILURE);
}
SDL_RenderPresent(renderer);
SDL_Delay(3000);
close_SDL();
exit(EXIT_SUCCESS);
}
I compiled the code with the command
gcc -g3 -o sdl_gfx_test sdl_gfx_test.c `sdl2-config --cflags --libs` -lSDL2_gfx
on Ubuntu 14.04
There were no compiler errors or warnings and also no linker errors. However no green rectangle appears on the screen. Only the white background is shown due to SDL_RenderClear(). I checked my code a dozen times but I could find no flaws. Have anyone an idea why boxColor() doesn't do his job?
Upvotes: 2
Views: 1000
Reputation: 2917
According to the documentation, your code is correct. But if you follow the given link to the code, you can see boxColor
doesn't exactly do what it says :
int boxColor(SDL_Renderer * renderer, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint32 color)
{
Uint8 *c = (Uint8 *)&color;
return boxRGBA(renderer, x1, y1, x2, y2, c[0], c[1], c[2], c[3]);
}
On a little endian system, a 32bit integer have the least significant byte first, so with a color 0xRRGGBBAA
, c[0]
is AA, c[1]
is BB, c[2]
is GG and c[3]
is RR.
Your program is actually displaying a transparent box.
You can avoid the problem by using the functions that take separate RGBA parameters.
Upvotes: 3