943,096 Members | Top Members by Rank

Ad:
Jan 31st, 2010
0

OpenGL/SDL

Expand Post »
im just trying to draw a 2D square but its not working and i dont know y?
main.cpp
#include "sdl.h"
#include "GL_Functions.h"

int SDL_main(int argc, char* argv[])
{
	//Used in the main loop
	bool Done = false;

	//Used to store SDL events
	SDL_Event event;

	//Lets open the window and initialise opengl
	InitGL(1024,768);

	while(!Done)
	{
		//Clear the screen
		glClear(GL_COLOR_BUFFER_BIT);


		glBegin(GL_QUADS);

		glColor3f(1,0,0);
		glVertex2i(256, 128);

		glColor3f(0,1,0);
		glVertex2i(768, 128);

		glColor3f(0,0,1);
		glVertex2i(768, 640);

		glColor3f(1,1,1);
		glVertex2i(256, 640);

		glEnd();

		//Present that freshly drawn scene to the screen
		SDL_GL_SwapBuffers();


		//While we have events (keyboard, mouse, window etc)
		while( SDL_PollEvent(&event) )
		{
			// if the window X was clicked on, ALT_F4 etc
			if ( event.type == SDL_QUIT )
				Done = true;

			//If we have had a key press
			if ( event.type == SDL_KEYDOWN )
			{
				//And the key was escape
				if ( event.key.keysym.sym == SDLK_ESCAPE )
					Done = true;

			}

		}
	}

	SDL_Quit();

	return 0;
}

the GL_Functions.h
#ifndef _GL_FUNCTIONS_H_
#define _GL_FUNCTIONS_H_

//Includes for sdl, opengl, sdl_image
#include "sdl_opengl.h"
#include "sdl.h"
#include "sdl_image.h"

//Used to initialise SDL and OpenGL
int InitGL(int Width, int Height, bool fullscreen = false);

#endif

and the GL_Functions.cpp

#include "GL_Functions.h"

int InitGL(int Width, int Height, bool fullscreen)
{
	int error;

	//Initialise SDL, with everything
	error = SDL_Init(SDL_INIT_EVERYTHING);

	//The following 6 lines setup the openGL settings
	//Setting up 32bit colour for the window
	//And to use Double buffering
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);



	//This stores the information we want to initialise our display window with
	//We want OpenGL and a Hardware surface so it will use the graphics card acceleration
	//Binary OR is used to add all of the flags together
	unsigned int flags = SDL_OPENGL | SDL_HWSURFACE | (fullscreen ? SDL_FULLSCREEN : 0);

	//Set the Video mode, with the correct Width, Height, bitdepth (32bit) to match our
	//OpenGL attributes we setup above.
	SDL_SetVideoMode(Width, Height, 32, flags);


	//Set the Clear Colour, this is the colour the window is cleared to as black
	glClearColor(0.0, 0.0, 0.0, 0.0);

	//Set the draw colour, to white.
	glColor4f( 1,1,1,1);

	//This is to setup an Ortho graphic projection. An ortho graphic projection has no perspective
	//It setups the projection so its Width and height matches the screen.
	//So 1 unit in world coordinates is 1 pixel on the screen.
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();
	glOrtho(0.0, Width, Height, 0.0, 0.0, 10.0);

	//Enable 2 dimensional textures
	glEnable(GL_TEXTURE_2D);


	//Enable blending (transparencies) and setup the blend function correctly.
	glEnable(GL_BLEND);
	glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

	//This complex horrid mess disables V-Sync.
	typedef void (APIENTRY * WGLSWAPINTERVALEXT) (int);
	WGLSWAPINTERVALEXT wglSwapIntervalEXT = (WGLSWAPINTERVALEXT) 
		wglGetProcAddress("wglSwapIntervalEXT");
	if (wglSwapIntervalEXT) {
		wglSwapIntervalEXT(0); // disable vertical synchronisation
	}

	return error;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
deorcar is offline Offline
4 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: Texture depth doesn't seem to work
Next Thread in Game Development Forum Timeline: Making Text Based Game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC