Starting sdl

Started by
0 comments, last by vbuser1338 18 years, 12 months ago
I am starting to learn a bit of sdl because even tho I am going to use opengl and already know a bit that sdl might come in useful sometimes so I thought I would learn. But the cone3d tutorial that I am using freezes up on my computer. Here is my cpp file.

#include <cstdlib>
#include <iostream>
#include <SDL.h>	//The sdl header file
#include <windows.h>
#include "main.h"

using namespace std;

int main(int argc, char *argv[])
{
	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)	//Init the video and audio
    {
		return 0;
	}
	atexit(SDL_Quit);	//Run SDL_Quit on exit
    SDL_Surface* screen;	//Pointer to the screen
    screen = SDL_SetVideoMode(640, 480, 32,	//Set up the screen
					SDL_HWSURFACE|SDL_DOUBLEBUF);
	if(screen == NULL)	//No screen made
	{
		MessageBox(NULL,"Could not set up screen.","ERROR",0);
		return 0;
	}
	
	int done=0;;	//Whether the loop is running
	while(done == 0);	//While done is false
	{
		SDL_Event event;	//Used to handle event
		while(SDL_PollEvent(&event))	//Poll events till none left
		{	
			if(event.type == SDL_QUIT)  	//Quit message
			{ 
				done = true;  
			}
			if(event.key.keysym.sym == SDLK_ESCAPE) //Escape
			{ 
				done = true; 
			}
		}
		DrawScene(screen);	//Go to drawing func
	}

	
	return 0;
}

void DrawScene(SDL_Surface *screen)	//Drawing func
{	
	Slock(screen);	//Lock the screen
	DrawPixel(screen,100,100,100,0,0);
	Sulock(screen);	//Lock the screen
	SDL_Flip(screen);	//Flip the screen
}

void DrawPixel(SDL_Surface *screen, int x, int y,
                                    Uint8 R, Uint8 G, Uint8 B)
{
  Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  switch (screen->format->BytesPerPixel)
  {
    case 1: // Assuming 8-bpp
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
        *bufp = color;
      }
      break;
    case 2: // Probably 15-bpp or 16-bpp
      {
        Uint16 *bufp;
        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *bufp = color;
      }
      break;
    case 3: // Slow 24-bpp mode, usually not used
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
        {
          bufp[0] = color;
          bufp[1] = color >> 8;
          bufp[2] = color >> 16;
        } else {
          bufp[2] = color;
          bufp[1] = color >> 8;
          bufp[0] = color >> 16;
        }
      }
      break;
    case 4: // Probably 32-bpp
      {
        Uint32 *bufp;
        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
        *bufp = color;
      }
      break;
  }
}

void Slock(SDL_Surface *screen)	//Used to lock the screen
{
  if ( SDL_MUSTLOCK(screen) )
  {
    if ( SDL_LockSurface(screen) < 0 )
    {
      return;
    }
  }
}

void Sulock(SDL_Surface *screen)	//Used to unlock the screen
{
  if ( SDL_MUSTLOCK(screen) )
  {
    SDL_UnlockSurface(screen);
  }
}

There is a header file but only has prototypes so thats not useful. It compiles fine and create a window but just freezes up and I can't exit. What is wrong with this code? Thanks, vbuser
Advertisement
Sorry fixed the problem it was a stupid one very stupid.
Cya later vbuser.

This topic is closed to new replies.

Advertisement