Advertisement Jump to content
Sign in to follow this  

OpenGL Blue Square on Red BG

This topic is 4936 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

All Im trying to do is blit a red square on a blue BG, the original code used Glut but I tried it using SDL. When I blit the square the whole screen turns red. Can anyone enlighten me on what I'm doing wrong. Any links to SDL-OGL documentation would be appreciated. Thanks.
#include <iostream>
#include <cstdio>
using namespace std;

#ifdef WIN32
#include <windows.h>

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

#define SCREEN_HEIGHT 600
#define SCREEN_WIDTH 800
#define SCREEN_BPP 16

int makewindow();
int RenderScene();

SDL_Surface *surface;
SDL_Event event;
float start_time,
GLboolean live = 1;

#ifdef WIN32
int main(int argc, char **argv){
int main(){

	if (makewindow()){
		cout << "Error: " << SDL_GetError() << endl;
		return 1;

	glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

		end_time = (float)SDL_GetTicks();
		elapsed_time = end_time - start_time;
		delta_time = elapsed_time / 1000;
		start_time = (float)SDL_GetTicks();
		// delta time

		while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                live = 0;
            case SDL_KEYDOWN:
                if (SDLK_ESCAPE == event.key.keysym.sym)
                    live = 0;

return 0;

int makewindow(){

	int videoFlags;
	const SDL_VideoInfo *videoInfo;

	    // initialize SDL
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	    cout << "Video initialization failed: " << SDL_GetError() << endl;
	    return 1;

    // Fetch the video info
    videoInfo = SDL_GetVideoInfo( );

    if ( !videoInfo )
	    cout << "Video query failed: " << SDL_GetError() << endl;
	    return 1;

    // the flags to pass to SDL_SetVideoMode
    videoFlags  = SDL_OPENGL;          // Enable OpenGL in SDL
    videoFlags |= SDL_GL_DOUBLEBUFFER; // Enable double buffering
    videoFlags |= SDL_HWPALETTE;       // Store the palette in hardware

    // This checks to see if surfaces can be stored in memory
    if ( videoInfo->hw_available )
	videoFlags |= SDL_HWSURFACE;
	videoFlags |= SDL_SWSURFACE;

    // This checks if hardware blits can be done
    if ( videoInfo->blit_hw )
	videoFlags |= SDL_HWACCEL;

    // Sets up OpenGL double buffering
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    // get a SDL surface
				videoFlags );

    // Verify there is a surface
    if ( !surface )
	    cout << "Video mode set failed: " << SDL_GetError() << endl;
	    return 1;

	return 0;

int RenderScene(){

	glColor3f(1.0f, 0.0f, 0.0f);
	glRectf(-25.0f, 25.0f, 25.0f, -25.0f);

return 0;

Share this post

Link to post
Share on other sites
first of all, you have to clear color buffer and depth buffer. Second, you are specifying coordinates like 25 - without setting the modelview matrix - which mean that theese coordinates are larger than max. device coordinates (1.0)

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!