Firstly, I am very new to graphics programming.
I am trying to get my program to display a block of colour on the screen.
The block of colour must be able to change to an arbitrary colour in about
1/40 s without any flicker and it must be able to do it repetitively (ie 40
times a second - at least). The colours are provided by other processing.
I have been playing with LibSDL to display a block that cycles through the
spectrum. Unfortunately, at resolutions above 320x240, it takes a painfully
long time to get anywhere (although it does do it very smoothly).
My next thought was to use the SDL_DBLBUFFER flag to speed up the displays
(if this is wrong - please tell me. As I said, I am new to graphics programming).
The call SDL_VideoModeOK returns 32 with SDL_DBLBUFFER flag, so I assume my
hardware is supporting everything ok (it is the NVidia Geforece 2 Go).
I then used that flag to define my video mode and replaced all the
SDL_UpdateRect(screen, 0, 0, 0, 0) with SDL_Flip(screen) and the program ran,
but no faster.
To test the SDL_Flip function, I inserted an error check after the call and lo,
there seems to be a problem!
I think I must be missing something really obvious. If so, could someone
please point it out. I’m also not sure I fully understand how SDL handles
screens etc.
Any help at all and suggestions on a better way to solve my problem would be
much appreciated.
I have attached the source I’ve been working on ‘test.c’
I have read the examples and the API reference in the DOCS.
Thanks
Henry Gomersall
/* test.c */
#include “SDL.h”
#include <stdio.h>
#define DISPLAY_RES_X 640
#define DISPLAY_RES_Y 480
int main() {
/* set various SDL variables */
SDL_Surface *screen;
SDL_Rect image;
Uint8 r, g, b;
/* Initilise SDL */
printf("Initializing SDL.\n");
/* Initialize Video */
if((SDL_Init(SDL_INIT_VIDEO)==-1)) {
fprintf(stderr, "Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
}
/* Initialize the SDL library */
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
if(SDL_VideoModeOK(DISPLAY_RES_X, DISPLAY_RES_Y, 32, SDL_HWSURFACE | SDL_DOUBLEBUF) != 32)
fprintf(stderr, "video mode not possible on this hardware\n");
/* Initialize the display in 32-bit palettized mode,
* requesting a hardware surface with double buffering */
screen = SDL_SetVideoMode(DISPLAY_RES_X, DISPLAY_RES_Y, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set %ix%ix8 video mode: %s\n", DISPLAY_RES_X, DISPLAY_RES_Y, SDL_GetError());
exit(1);
}
for(;;){
for(g = 0;g<255;g++) {
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, g, 0));
if(!(SDL_Flip(screen))){
fprintf(stderr, "error flipping screen buffers\n");
}
}
for(r = 255;r>0;r--) {
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, 255, 0));
SDL_Flip(screen);
}
for(b = 0;b<255;b++) {
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 255, b));
SDL_Flip(screen);
}
for(g = 255;g>0;g--) {
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, g, 255));
SDL_Flip(screen);
}
for(r = 0;r<255;r++) {
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, r, 0, 255));
SDL_Flip(screen);
}
for(b = 255;b>0;b--) {
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 0, b));
SDL_Flip(screen);
}
}
printf("SDL initialized.\n");
printf("Quiting SDL.\n");
/* Shutdown all subsystems */
SDL_Quit();
printf("Quiting....\n");
exit(0);
}