[SDL] SDL fullscreen problem
Torsten Giebl
wizard at syntheticsw.com
Tue Sep 4 01:35:08 PDT 2007
Hello !
>> When I run a program in X system on F7 ,the X crashed ,just like I
>> press
>> CTRL+ALT+BACKSPACE.
>> It is sure that the code has no problem,I get it from linux game
>> programming.
>> What's the problem?
>>
>>
>> the code is below,
>>
>> #include <SDL/SDL.h>
>> #include <stdio.h>
>> #include <stdlib.h>
Use #include "SDL.h" and use it after stdio.h and stdlib.h
>> Uint16 CreateHicolorPixel(SDL_PixelFormat * fmt, Uint8 red,
>> Uint8 green, Uint8 blue)
>> {
>> Uint16 value;
You use Uint16 but you are running with SDL_SetVideoMode (..,24,..)
so better use a Uint32 for pixel values.
>> value = ((red >> fmt->Rloss) << fmt->Rshift) +
>> ((green >> fmt->Gloss) << fmt->Gshift) +
>> ((blue >> fmt->Bloss) << fmt->Bshift);
>> return value;
>> }
Better use the SetPixel function from the SDL website.
>> int main()
To be portable do int main(int argc, char *argv [])
>> {
>> SDL_Surface *screen;
>> Uint16 *raw_pixels;
>> int x, y;
>> if (SDL_Init(SDL_INIT_VIDEO) != 0) {
>> printf("Unable to initialize SDL: %s\n", SDL_GetError());
>> return 1;
>> }
>> atexit(SDL_Quit);
>> screen = SDL_SetVideoMode(256, 256, 24,
>> SDL_FULLSCREEN|SDL_SWSURFACE);
>> if (screen == NULL) {
>> printf("Unable to set video mode: %s\n", SDL_GetError());
>> return 1;
>> }
Do an if SDL_MUSTLOCK (screen) true then only lock the surface.
>> SDL_LockSurface(screen);
>> raw_pixels = (Uint16 *) screen->pixels;
>> for (x = 0; x < 256; x++) {
>> for (y = 0; y < 256; y++) {
>> Uint16 pixel_color;
>> int offset;
>> pixel_color = CreateHicolorPixel(screen->format,
>> x, 0, y);
>> offset = (screen->pitch / 2 * y + x);
>> raw_pixels[offset] = pixel_color;
>> }
>> }
>> SDL_UnlockSurface(screen);
The same with SDL_MUSTLOCK as above
>> SDL_UpdateRect(screen, 10, 10, 200, 120);
>> SDL_Delay(3000);
>> return 0;
>> }
CU
More information about the SDL
mailing list