Problems With Sdl


G Morgan

Still Fresh
Joined
Jan 31, 2006
Messages
65
Age
39
Location
South Wales
Website
Visit site
Doing the tutorial from this site. I can't get the coloured screen to come up. The black screen worked fine but when attempting to get to the next stage the black screen was still all I got. I'm using Dev-cpp v4 but have also tried it in the lastest v5 beta with the same result.

I have compared my code to what it says on the site about 5 times line by line and it all seems to be correct but obviously something is wrong otherwise I'd see colours.

Anyone had problems with this sort of thing before and if so how did you solve it.
 
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

Its probably this line causing the problem. the 640,480 refers to the screen size which should be 320,240. also, unless you are using the HW Accelerated libs (SDL_HWSURFACE) the program has crashed straight away with black screen which is kinda tricking you into thinking the screen displayed black on purpose. its probably the screen size that is the problem though.

try changing it to

screen = SDL_SetVideoMode(320, 240, 32, SDL_SWSURFACE);
 
I'm trying to run it in XP so far but will try changing the settings to match what it is in my XP desktop. I have tried both that and using software acc in the past but not both at the same time. Does any hardware have an issue with hw acc in SDL. I'm using a Geforce 6800 softmodded to 16p 6v but have also returned it to the factory default (12p 5v) incase that was the problem. The drivers are the lastest.
 
I've tried in full screen yes. Have tried altering the resolutions and used both hardware and software calls in both. Have also tried replacing the DrawPixel function with this one I got from the sites forum

Code:
void SetPixel ( SDL_Surface* pSurface , int x , int y , int r, int g, int b ) 
{
  //convert color
  Uint32 col = SDL_MapRGB ( pSurface->format , r , g , b );

  //determine position
  char* pPosition = ( char* ) pSurface->pixels;

  //offset by y
  pPosition += ( pSurface->pitch * y );

  //offset by x
  pPosition += ( pSurface->format->BytesPerPixel * x );

  //copy pixel data
  memcpy ( pPosition , &col , pSurface->format->BytesPerPixel );
}

Which doesn't seem to work either. I think I'll try to run it on another machine, if it works there then I know the problem is with this machine rather than with the code itself.
 
Have now tried this on a second machine and it still doesn't work the code I've been using is this.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <SDL/SDL.h>

void SetPixel ( SDL_Surface* pSurface , int x , int y , int r, int g, int b );

void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *screen);

int main(int argc, char *argv[])
{
 if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
  {
   printf("Unable to init SDL: %s\n", SDL_GetError());
   exit(1);
  }
 atexit(SDL_Quit);

 SDL_Surface *screen;
 screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
 if ( screen == NULL )
  {
   printf("Unable to set 640x480 video: %s\n",SDL_GetError());
   exit(1);
  }
 // DRAWING GOES HERE
 Slock(screen);
 for( int x=0; x<640; x++ )
  {
   for( int y=0; y<480; y++ )
    {
     SetPixel( screen, x, y, y/2, y/2, x/3 );
    }
  }
 Sulock(screen);
 SDL_Flip(screen);

 return 0;
}

void SetPixel ( SDL_Surface* pSurface , int x , int y , int r, int g, int b ) 
{
  //convert color
  Uint32 col = SDL_MapRGB ( pSurface->format , r , g , b );

  //determine position
  char* pPosition = ( char* ) pSurface->pixels;

  //offset by y
  pPosition += ( pSurface->pitch * y );

  //offset by x
  pPosition += ( pSurface->format->BytesPerPixel * x );

  //copy pixel data
  memcpy ( pPosition , &col , pSurface->format->BytesPerPixel );
}

void Slock(SDL_Surface *screen)
 {
  if ( SDL_MUSTLOCK(screen) )
   {
    if (SDL_LockSurface(screen) < 0 )
     {
      return;
     }
   }
 }

void Sulock(SDL_Surface *screen)
 {
  if ( SDL_MUSTLOCK(screen) )
   {
    SDL_UnlockSurface(screen);
   }
 }

Everything I check suggests this should work yet it doesn't on either machine. It leaves a strange situation. The code seems correct yet it fails on two machines which suggests the fault is in the code. If somebody can see a flaw I'm missing then please point it out and I'll be forever grateful.
 
looking at the last bit of code, you flip the screen, then immediately return from main, which would quit sdl etc, so you leave no time to actually see the results? why not sleep(10) first?

P.
 
luteijn posted on Feb 3 2006 at 07:02 AM said:
looking at the last bit of code, you flip the screen, then immediately return from main, which would quit sdl etc, so you leave no time to actually see the results? why not sleep(10) first?

P.


I assume sleep(10) is a C++ command (otherwise I'm going to look extremely stupid). Have a fair amount of knowledge about C but had intended on picking up the C++ stuff as I went along (not a great method I know). If I spend the next week doing a crash course hopefully I'll be in a better position to continue.
 
Last edited by a moderator:
G Morgan posted on Feb 4 2006 at 02:59 AM said:
I assume sleep(10) is a C++ command (otherwise I'm going to look extremely stupid).

It's C. If you learn C++ in a crash course, you'll probably end up using only a few of its features and end up speaking some sort of inbetween version of the 2 languages.

P.
 
Last edited by a moderator:
Seriously, I actually bought a text book to learn C and there was no mention of it. Considering my C knowledge is obviously very basic and I eventually intend to learn C++ anyway is it really worth learning the later C commands or am I better off simply forgeting it and going strictly C++ from here on in.

My idea of a crash course is 10+ hours a day, my GP2X arrives on monday and I intend on being close to getting back to SDL by next Saturday preferably. Learning by doing is better anyway and my code would clear up over time (I hope) before anything workable comes out of it.

I doubt I will get confused, I sucessfully forgot Pascal from my A-level course at the end of the day so could easily forget the printf command if needs be :) (tbh though I can still remember writeln so I haven't really forgotten that much) .
 
get a better C book. Or better yet a book that teaches programming, not just an implementation language. Then decide what style of programming suits you (imperative, object-oriented, or functional), and pick a language based on that, not on what is perceived to be the 'kewl' language. You'll get better results with a language that fits you, instead of trying to fit to a language..

C++, as the name suggests, builds on C. some things are done completely different, but you'll see many programs written in 'C++' that still use printf("hello\n"); instead of cout<<"hi"<<endl;. If you do't have a solid footing in C, it will bite you in the end.

C and pascal are actually quite similar. C is just more terse and lets you do things that pascal protects you against. Pascal is good for learning to progam, as it's clearer to most people.

P.
 
Back
Top