Jump to content
  • Advertisement
Sign in to follow this  
SoNok0

OpenGL Blue Square on Red BG

This topic is 4866 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>
#endif

#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,
end_time,
elapsed_time,
delta_time;
GLboolean live = 1;

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

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

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

	while(live){
		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;
                break;
            case SDL_KEYDOWN:
                if (SDLK_ESCAPE == event.key.keysym.sym)
                    live = 0;
			}
		}
		RenderScene();
		SDL_GL_SwapBuffers();
	}

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;
    else
	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
    surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
				videoFlags );

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

	return 0;
}

int RenderScene(){

	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
	glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
	glFlush();

return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
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 GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!