Archived

This topic is now archived and is closed to further replies.

c-gibson-s

SDL .bmp displaying..or any display at all?

Recommended Posts

Right, here is my code. It just displays a black box. Im going through an SDL tutorial over at cone3D. Grahh. the images are in the right directories, and im..er. lost. new. yeah..help please!
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

#include <stdio.h>
#include <stdlib.h>

#include <windows.h>
#include <windowsx.h>

#include "SDL.h"  

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B);
void sLock( SDL_Surface *screen );
void sUlock( SDL_Surface *screen);
void DrawScene();
int ImageInit();
void DrawImage( SDL_Surface *image, int x, int y);
void DrawImage( SDL_Surface *image, int x, int y, int w, int h, int x2, int y2);
void DrawBG();

SDL_Surface *screen;
SDL_Surface *image;
SDL_Surface *back;

int xpos = 0, ypos = 0;

int main( int argc, char* argv[] ) {
   
   Uint8* key;

   if( SDL_Init( SDL_INIT_VIDEO ) == ( -1 ) ) 
   { 
      printf("Could not initialize SDL: %s.\n", SDL_GetError());
      exit(-1);
   }

   /* Set up the video */
  
   int WINWIDTH = 640, WINHEIGHT = 480, BPP = 32;
   screen = SDL_SetVideoMode( WINWIDTH, WINHEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF );
   if( screen == NULL ) 
   {
        fprintf(stderr, "Couldn''t set %ix%ix%i video mode: %s\n", WINWIDTH,WINHEIGHT,BPP, SDL_GetError());
        exit(1);
   }
   atexit(SDL_Quit);

   ImageInit();
   DrawBG();

   int done = 0;

   sLock ( screen );
   while ( done == 0 )
   {
	   SDL_Event event;

	   while ( SDL_PollEvent( &event ) )
	   {
		   key = SDL_GetKeyState( NULL );
		   if ( key[SDLK_UP] ) { ypos -= 1; }
		   if ( key[SDLK_DOWN] ) { ypos += 1; }
		   if ( key[SDLK_RIGHT] ) { xpos += 1; }
		   if ( key[SDLK_LEFT] ) { xpos -= 1; }
		   if ( key[SDLK_ESCAPE] ) { done = 1; }
		   DrawScene();
	   }
	   
   }

   sUlock( screen ) ;
		   


   exit(0);
}

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:
		{
			Uint8 *bufp;
			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
			*bufp = color;
		}
		case 2:
		{
			Uint16 *bufp;
			bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
			*bufp = color;
		}
		case 3:
		{
			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;
			}
		}
		case 4:
		{
			Uint32 *bufp;
			bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
			*bufp = color;
		}
		break;
	}
}

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);
	}
}

void DrawScene()
{
   DrawImage( back, xpos - 2, ypos - 2, 132, 132, xpos - 2, ypos - 2);
   DrawImage( image, xpos, ypos );
   SDL_Flip( screen );
}


int ImageInit()
{
	back = SDL_LoadBMP("Fonts/bg.bmp");
	image = SDL_LoadBMP("Fonts/image.bmp");
	return 0;
}

void DrawImage( SDL_Surface *image, int x, int y)
{
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface( image, NULL, screen, &dest );
}

void DrawImage( SDL_Surface *image, int x, int y, int w, int h, int x2, int y2)
{
	SDL_Rect dest;
	SDL_Rect dest2;
	dest.x = x;
	dest.y = y;
	dest2.x = x2;
	dest2.y = y2;
	dest2.w = w;
	dest2.h = h;
	SDL_BlitSurface ( image, &dest2, screen, &dest );
}

void DrawBG()
{
	DrawImage( back, 0, 0 );
}

Share this post


Link to post
Share on other sites
don''t lock the surface when you are blitting, only when you access the raw pixels. Also, lock the surface only when you need access it''s pixels and unlock it right after you''ve finish.

hope that helps !
Matt

Share this post


Link to post
Share on other sites