1 Replies - 8301 Views - Last Post: 11 December 2010 - 10:52 AM

#1 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Week #44- SDL

Post icon  Posted 08 December 2010 - 06:55 AM

This week's challenge is SDL, written by stayscrisp.

Posted Image

SDL is a cross platform graphics library written in C++. There are also a number of extension libraries available that enable network and audio functionality. SDL is a great way to add graphics to your application whilst hiding the platform specific details, it can even create OpenGL windows for 3D graphics.

SDL has been used in many commercial projects including Second Life, Neverwinter Nights and World of Goo. Many games are ported to Linux using SDL due to its cross-platformness (it's a word!).

Getting Started



Resources



It would be great to see some simple games such as Pong, Arkanoid or Asteroids or some none game related graphics.

Is This A Good Question/Topic? 0
  • +

Replies To: Week #44- SDL

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1010
  • View blog
  • Posts: 4,214
  • Joined: 14-February 08

Re: Week #44- SDL

Posted 11 December 2010 - 10:52 AM

A simple program to initialise SDL, create a window and display a BMP image.

/*
Create a window and blit an SDL surface to it
*/

#include <SDL.h>
#include <string>
#include <iostream>

bool g_bRunning = false; 
bool g_bFullscreen = false;
SDL_Surface* g_pScreen = NULL;
SDL_Surface* g_pTestSurface = NULL;

SDL_Surface* LoadImage(std::string filename)
{
	// pointers to some SDL surfaces
	SDL_Surface* pTempSurface = NULL;
	SDL_Surface* pReturnedSurface = NULL;

	// create the surface from the loaded BMP and point to it through pTempSurface
	if((pTempSurface = SDL_LoadBMP(filename.c_str())) == NULL)
	{
		std::cout << "Could not open file" << filename << "\n";
		return NULL; // return nothing
	}

	// set the display format of the final surface using the temporary surface
	pReturnedSurface = SDL_DisplayFormatAlpha(pTempSurface);

	// free the temporary surface
	SDL_FreeSurface(pTempSurface); 

	// return a pointer to the final surface
	return pReturnedSurface;
}

// creates a rectangle to blit the SDL_Surface to
bool DrawSurface(SDL_Surface* dest, SDL_Surface* src, float x, float y)
{
	if(dest == NULL || src == NULL)
	{
		return false;
	}

	SDL_Rect  destR;

	destR.x = (Sint16)x;
	destR.y = (Sint16)y;

	SDL_BlitSurface(src, NULL, dest, &destR);

	return true;
}

// Very simple event management, if the cross is clicked the program ends
void HandleEvents()
{
	SDL_Event event;

	if (SDL_PollEvent(&event)) 
	{
		switch (event.type) 
		{
		case SDL_QUIT:
			g_bRunning = false;
			break;
		}
	}
}

bool InitialiseSDLWindow(const std::string title, int width, int height, int bpp, bool fullscreen)
{
	// used for fullscreen in this instance
	int flags = 0;

	// initialise SDL video capabilities
	if(SDL_Init(SDL_INIT_VIDEO) == 1) 
	{
		std::cout << "Could not init SDL\n";
		return false;
	}

	// Set the window title
	SDL_WM_SetCaption(title.c_str(),title.c_str());

	if(fullscreen) // set fullscreen if wanted
	{
		flags = SDL_FULLSCREEN;
	}

	// Create the screen surface
	g_pScreen = SDL_SetVideoMode(width, height, bpp, flags);

	g_bFullscreen = fullscreen;

	g_bRunning = true;

	return true;
}

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

	if(!InitialiseSDLWindow("Test", 800, 600, 32, false))
	{
		std::cout << "Could not init SDL window\n";
		return 1;
	}

	// load the surface
	g_pTestSurface = LoadImage("test.bmp");
	if(g_pTestSurface == NULL) // error checking ;)/>
	{
		std::cout << "Could not open image\n";
		return 1;
	}

	// main loop
	while(g_bRunning)
	{
		HandleEvents();
		DrawSurface(g_pScreen, g_pTestSurface, 100, 100);
		SDL_Flip(g_pScreen);
	}

	return 0;
}



I thought I'd put in error checking unlike my tutorials which left it out for the sake of clarity :) Hopefully this might help some people interested in having a go at SDL
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1