Archived

This topic is now archived and is closed to further replies.

clum

Translating from GLUT to SDL

Recommended Posts

clum    217
Hi, I've been translating a game I wrote in GLUT to SDL. I wrote a very basic SDL/OpenGL framework, based on a few examples I saw. I'm having two problems. First of all, my GL_QUAD simply isn't appearing. I copy and pasted the actual GL code to make a nice (or not so nice, I'm eventually going to replace it with a picture) background, and its not going to the screen! The other problem is that I'm clearly |'ing SDL_FULLSCREEN and yet it still appears in a window. What am I doing wrong? I compiled it with: g++ main.cpp `sdl-config --libs` -lGL -lGLU -o sdltemplate Here's the code (I admit to using KDevelop, who would actually write in the time like that?):
/***************************************************************************
                          main.cpp  -  description
                             -------------------
    begin                : Mon Aug 11 17:38:16 EDT 2003
    copyright            : (C) 2003 by Shalom Naumann

 ***************************************************************************/

#include <iostream>
#include <cstdlib>
using namespace std;

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

// Prototypes

void sdlexit(int exitNum);
void drawScene();

// The screen

SDL_Surface *surface;

int main()
{

    // Initializes the SDL API

    if (SDL_Init(SDL_INIT_VIDEO)<0) {
        cerr << "Cannot initialize SDL video. " << SDL_GetError() << ".\n";
        sdlexit(1);
    }

    // Gets info about display

    const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
    if (!videoInfo) {
        cerr << "Cannot find information about display device. " << SDL_GetError() << "\n";
        sdlexit(1);
    }
        
                                                                        
    // Sets up double buffering

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    // Defines parameter to next function

    int videoFlags = SDL_OPENGL|SDL_GL_DOUBLEBUFFER|SDL_HWPALETTE|SDL_FULLSCREEN|
        (videoInfo->hw_available)?SDL_HWSURFACE:SDL_SWSURFACE;
    if (videoInfo->blit_hw) videoFlags |= SDL_HWACCEL;

    // Defines the SDL surface as the screen

    surface = SDL_SetVideoMode(1024, 768, 16, videoFlags);
    if (!surface) {
        cerr << "Cannot set video mode. " << SDL_GetError() << "\n";
        sdlexit(1);
    }

    // Initializes OpenGL matrices

    glMatrixMode(GL_PROJECTION);
    glOrtho(0, 1024, 0, 768, -1, 1);
    glMatrixMode(GL_MODELVIEW);

    SDL_Event event;    // Used in the game loop to receive SDL events


    // The game loop

    bool done=false;
    while(!done) {
        // Check for SDL events - inputs and processes

        while(SDL_PollEvent(&event)) {
            switch(event.type) {
            case SDL_KEYDOWN:
                done=true;
            }
        }

        // Outputs

        drawScene();
    }    

    sdlexit(0);
    return 0;
}

// Cleans up and exits - replaces standard exit funtion 

void sdlexit(int exitNum){
    cout << "This SDL program is now exiting with code " << exitNum << "\n";
    SDL_Quit();
    exit(exitNum) ;
}

// Draws the scene (brilliant comment)

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_QUADS);
		glColor3d(1.0,0.0,0.0);
		glVertex3d(0,0,.5);

        glColor3d(1.0,1.0,1.0);
		glVertex3d(1024,0,.5);

		glColor3d(0.0,1.0,0.0);
		glVertex3d(1024,768,.5);

		glColor3d(0.0,0.0,1.0);
		glVertex3d(0,768,1);
    glEnd();

    
    SDL_GL_SwapBuffers();   // Display double buffer on screen

}
Is there anything utterly wrong with code? If you compile it, does it work? BTW, I'm using SuSE 8.2 with an nVidia (yes, I installed their drivers) GeForce 2. Thanx in advance. [edited by - clum on August 12, 2003 2:01:05 PM]

Share this post


Link to post
Share on other sites
clum    217
BTW, I tried it in Windows (on the same machine) and it had the same problems - nothing appeared and it wasn''t full screen (though it did cover the whole screen, it had a title bar).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You havn''t set the viewport (or the ''camera'' position)

glViewport(0,0,1024,768); should do the trick.

SDL_FULLSCREEN works for me, but I''m using windows, so it could be a driver issue. perhaps.

Share this post


Link to post
Share on other sites