• Advertisement
Sign in to follow this  

Starting sdl

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement