Jump to content


SDL and OpenGL example doesn't compile


1 reply to this topic

#1 cypher543

    Member

  • Members
  • PipPip
  • 74 posts

Posted 21 November 2006 - 12:31 AM

I've been putting this together for the last 45 minutes or so, learning how OpenGL and SDL go together. So far, I think I have working source code, but I just can't get it to compile. G++ keeps leaving the SDL libraries out!

engine/main.cpp:
#include "pch.h"

void quit() {
	SDL_ShowCursor(1);
	SDL_quit();
	exit(0);
}

SDL_Surface *screen = NULL;
int scrw = 640, scrh = 480;

int main(int argc, char **argv) {
	#define log(s) puts("initializing " s)

	log("SDL");
	if (SDL_Init(SDL_INIT_VIDEO)) {
		puts("Unable to initialize SDL!");
	}

	log("Video");
	screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen==NULL) {
		puts("Unable to create OpenGL screen!");
	}

	log("Misc");
	SDL_WM_SetCaption("game engine", NULL);
    SDL_ShowCursor(0);

	log("Main Loop");
	SDL_event event;
	bool done = false;
	while (!done) {
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				done = true;
			} else if (event.type == SDL_KEYDOWN) {
				if (event.key.keysym.sym == SDLK_ESCAPE) {
					done = true;
				}
			}
		}
	}
	quit();
	return 0;
}
shared/pch.h:
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <GL/glext.h>
Makefile:
CXX = g++
CXXOPTFLAGS = -Wall -fsigned-char -O3 -fomit-frame-pointer
CXXFLAGS = $(CXXOPTFLAGS) -I. -Iengine -Ishared `sdl-config --cflags --libs` -lGL -lGLU
OBJS = engine/main.o

default: all

all: $(OBJS)
	$(CXX) $(CXXFLAGS) -o game_engine $(OBJS)
I figured it had something to do with `sdl-config --cflags --libs`, so I replaced it with -lSDL, but it spit literally hundreds of errors at me. I understand this may seem like a really stupid problem, and I'm sorry to waste your time. But, can anyone tell me what the problem is?

Thanks!

#2 SamuraiCrow

    Senior Member

  • Members
  • PipPipPipPip
  • 459 posts

Posted 22 November 2006 - 09:48 PM

Well, for one thing, you're opening a standard SDL screen instead of an OpenGL screen...

Try putting an SDL_OPENGL | SDL_HWSURFACE in the place of SDL_HWSURFACE | SDL_DOUBLEBUF and use SDL_GL_SwapBuffers() in the place of SDL_Flip(). Also, to get double-buffering in an OpenGL screen in SDL, you have to do a SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); after the screen is opened.

http://www.libsdl.or...sdldoc-html.zip will help you with the OpenGL functions a little bit since they differ from ordinary OpenGL in a few places (specifically SDL_GL_SetAttribute() and SDL_GL_GetAttribute() ).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users