All pastes #715121 Raw Edit

Untitled

public text v1 · immutable
#715121 ·published 2007-09-26 01:27 UTC
rendered paste body

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/glu.h>

#ifdef main
#undef main
#endif

class WRS_SDL
{
	bool running;
	public:
	WRS_SDL();
	~WRS_SDL();
	void init(int w, int h, const char *title);
};

WRS_SDL::WRS_SDL()
{
	running = false;
}

WRS_SDL::~WRS_SDL()
{
	if(running)
	{
		SDL_Quit();
	}
}

void WRS_SDL::init(int w, int h, const char *title)
{
	running = true;
	if(SDL_Init( SDL_INIT_VIDEO ) < 0)
		throw __LINE__;
	int rgb_size[3] = {5, 5, 5};	// for 16 bit video. try also 8,8,8 for 24/32 bit video
	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] );
	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] );
	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] );
	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
	const SDL_VideoInfo *info = SDL_GetVideoInfo();
	if ( SDL_SetVideoMode( w, h, info->vfmt->BitsPerPixel, SDL_OPENGL | SDL_SWSURFACE) == NULL )		// why SDL_SWSURFACE?
		throw __LINE__;
	SDL_WM_SetCaption(title, title);
	
	glViewport(0,0,w,h); 
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, w, 0, h, -1.0, 1.0);		// -1.0, +1.0 ???
	glClearColor(0.0,0.0,0.0,0.0);
	SDL_ShowCursor(0);
}

class Bat
{
	int width, height;
	int bat_w, bat_h;
	int bat_x, bat_y;
	int speed;

	public:
	Bat(int w, int h);
	void top();
	void bottom();
	void draw();
	void update();
	void move(char direction, bool keydown);
};

Bat::Bat(int w, int h)
{
	width = w;
	height = h;
	bat_w = 100;
	bat_h = 10;
	speed = 0;
}

void Bat::top()
{
	bat_x = width / 2 - bat_w / 2;
	bat_y = height - bat_h;
}

void Bat::bottom()
{
	bat_x = width / 2 - bat_w / 2;
	bat_y = 0;
}

void Bat::draw()
{
	glBegin(GL_POLYGON);
	glColor3f(1.0, 1.0, 1.0);
	glVertex2i(bat_x, bat_y + bat_h - 1);
	glVertex2i(bat_x + bat_w - 1, bat_y + bat_h - 1);
	glVertex2i(bat_x + bat_w - 1, bat_y);
	glVertex2i(bat_x, bat_y);
	glEnd();
}

void Bat::update()
{
	int old_bat = bat_x;
	bat_x += speed;
	if(bat_x < 0 || bat_x + bat_w >= width)
	{
		speed = 0;
		bat_x = old_bat;
	}
}

void Bat::move(char direction, bool keydown)
{
	if(keydown)
	{
		speed = 1;
		if(direction == 'L')
			speed = -speed;
	}
	else
	{
		speed = 0;
	}
}

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
	try
	{
		WRS_SDL sdl;
		const int width = 800, height = 600;
		sdl.init(width, height, argv[0]);
		Bat ubat(width, height);
		Bat cbat(width, height);
		ubat.bottom();
		cbat.top();
		
		bool done = false;
		while(!done)
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			ubat.draw();
			cbat.draw();
			SDL_GL_SwapBuffers();
			
			//SDL_Delay(20);
			
			ubat.update();
			cbat.update();
		
			SDL_Event event;
			while( SDL_PollEvent( &event ) ) {
				switch(event.type)
				{
					case SDL_KEYDOWN:
					case SDL_KEYUP:
						switch(event.key.keysym.sym)
						{
							case SDLK_ESCAPE:
								if(event.type == SDL_KEYDOWN)
									done = true;
								break;
							case SDLK_LEFT:
								ubat.move('L', event.type == SDL_KEYDOWN);
								break;
							case SDLK_RIGHT:
								ubat.move('R', event.type == SDL_KEYDOWN);
								break;
						}
						break;
					case SDL_QUIT:
						done = true;
						break;
				}
			}
		}

	}
	catch(int line)
	{
		cout << "Error, line " << line << endl;
	}
	return 0;
}