deorcar 0 Newbie Poster

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

Dani AI

Generated

A few focused checks and small fixes will usually reveal why a simple GL quad never appears.

  • Verify window/GL context creation succeeded. Check the return values from SDL_Init and SDL_SetVideoMode and print SDL_GetError() if they fail; if the video mode step fails there is no valid GL context to draw into (see SDL_SetVideoMode docs: ).
  • Make the viewport and modelview explicit after creating the context. Call glViewport(0, 0, Width, Height) and then switch back to the modelview matrix with glMatrixMode(GL_MODELVIEW); glLoadIdentity(); before issuing primitives. If you omit glViewport the default may not match your window, and if the modelview is not identity previous transforms can move geometry offscreen (see glViewport reference: glViewport).
  • Texturing is enabled in InitGL. If no texture is bound, or you are not supplying texture coordinates, textured rendering can override flat colors. Either disable texturing when drawing untextured quads (glDisable(GL_TEXTURE_2D)) or provide texcoords and a valid texture.
  • Add basic runtime checks: call glGetError() after initialization and after drawing to catch GL errors early. Also confirm SDL_GL_SwapBuffers is being reached (logging helps).
  • Minor points to rule out: run the program with a simple clear color change to confirm buffer swaps work; ensure the entry point used matches your SDL version (main vs SDL_main) and that you link SDLmain if required.

These steps isolate whether the problem is SDL/video-mode/context creation, a missing viewport/modelview setup, or texturing state interfering with vertex colors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.