2015-10-16 156 views
1

我想在SDL2和SDL2_gfx庫的屏幕上繪製一些圖形原語。但不知何故,SDL2_gfx函數似乎不起作用。下面的代碼應該繪製白色背景上的簡單的綠色填充矩形SDL2_gfx函數不起作用

#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); 
} 

我編寫的代碼的命令

gcc -g3 -o sdl_gfx_test sdl_gfx_test.c `sdl2-config --cflags --libs` -lSDL2_gfx 

在Ubuntu 14.04

目前還沒有編譯器錯誤或警告也沒有鏈接器錯誤。但屏幕上不會出現綠色矩形。由於SDL_RenderClear(),只顯示白色背景。我檢查了我的代碼十幾次,但是我發現沒有任何缺陷。有沒有人有一個想法,爲什麼boxColor()不做他的工作?

回答

1

根據the documentation,您的代碼是正確的。但是,如果你按照給定鏈路的代碼,你可以看到boxColor不正是做什麼它說:

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]); 
} 

在小端系統,32位整數首先有至少顯著字節,所以用顏色0xRRGGBBAA,c[0]是AA,c[1]是BB,c[2]是GG和c[3]是RR。

你的程序實際上是顯示一個透明的框。

您可以通過使用帶有單獨RGBA參數的函數來避免該問題。