libsegl/main.cpp

178 lines
4.1 KiB
C++

#define VERSION "0.2.2 DEV"
#include <iostream>
#include <string>
#include <SDL.h>
#include <SDL_opengl.h>
#include "gltexture.h"
#include "glfontengine.h"
// #define GL_DEBUG_MOVEMENT false
void setupGL();
void resizeOpenGLWindow(int, int, float=1.0f, float=100.0f);
int main(int argc, char **argv) {
std::cout << "ARGV: " << argv[0] << std::endl;
//Vars
SDL_Surface *screen;
bool quit = false;
SDL_Event event;
int w_width = 640;
int w_height = 480;
int w_bbp = 32;
float znear = 1.0f;
float zfar = 100.0f;
if(SDL_Init(SDL_INIT_VIDEO)<0) {
std::cerr << "Konnte SDL nicht initialisieren" << SDL_GetError() << std::endl;
return 1;
}
// Setting video mode
int videoFlags;
const SDL_VideoInfo *videoInfo;
videoInfo = SDL_GetVideoInfo();
if(!videoInfo) {
std::cerr << "Video query failed: " << SDL_GetError() << std::endl;
return 1;
}
videoFlags = SDL_OPENGL;
videoFlags |= SDL_GL_DOUBLEBUFFER;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_RESIZABLE;
if(videoInfo->hw_available)
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;
if(videoInfo->blit_hw)
videoFlags |= SDL_HWACCEL;
// videoFlags |=SDL_FULLSCREEN;
// SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
// SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
// SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if(!(screen = SDL_SetVideoMode(w_width, w_height, w_bbp, videoFlags))) {
std::cerr << "Kein SDL-Screen verfügbar: " << SDL_GetError() << std::endl;
return 1;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_WM_SetCaption("GLGui Test, 0);
// blank screen
// SDL_FillRect(screen, 0, SDL_MapRGBA(screen->format, 0, 0, 0, 0));
// GL Optionen
setupGL();
resizeOpenGLWindow(w_width, w_height, znear, zfar);
// Keyrepeat, falls benötigt auskommentieren
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
// Fontengine initliaisieren, für alle die Fonts brauchen
GLFontEngine::addFont("font_big_better_alpha.png", "default");
// Programmvariablen
Snake3D snake3d;
//movement & keyboardcontrol
Uint8 *keys = SDL_GetKeyState(NULL);;
SDLKey tkey;
typedef enum { move_up=0, move_down, move_left, move_right} nummovekeys;
SDLKey movekeys[4];
bool pause = true;
bool blend = false;
bool extralines = false;
bool fullscreen = false;
// splash screen
bool splashscreen = true;
snake3d.pause(true);
SDL_Rect splashpos = { 40, 40, screen->w -80, 310 };
GLFontEngine fontengine("default");
#ifdef GL_DEBUG_MOVEMENT
movekeys[move_up] = SDLK_i;
movekeys[move_down] = SDLK_k;
movekeys[move_left] = SDLK_j;
movekeys[move_right] = SDLK_l;
#else
movekeys[move_up] = SDLK_UP;
movekeys[move_down] = SDLK_DOWN;
movekeys[move_left] = SDLK_LEFT;
movekeys[move_right] = SDLK_RIGHT;
#endif
while(!quit) {
if(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
keys = SDL_GetKeyState(NULL);
tkey = event.key.keysym.sym;
break;
case SDL_KEYUP:
keys = SDL_GetKeyState(NULL);
break;
}
} else {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
SDL_Delay(10);
}
}
GLFontEngine::quit();
SDL_Quit();
return 0;
}
void setupGL() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
// glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
// glBlendFunc(GL_DST_COLOR, GL_SRC_ALPHA);
// glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1);
}
void resizeOpenGLWindow(int width, int height, float znear, float zfar) {
if(height==0)
height = 1;
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}