wojownik266 Temat założony przez niniejszego użytkownika |
[AllegroGL] Podstawowy Szkielet Applikacji » 2010-02-13 13:47:30 Gdzieś tam w czeluściach internetu wygrzebałem kawałek kodu allegrogl i trochę go zmodernizowałem. Kod po modernizacji prezentuję nieco niżej. A tutaj zadam pytanie - czy ten kod mozna potraktować jako punkt wyjscia do dalszej rozbudowy majacej na celu stworzenie jakiejś prostej gry 3D? Jednym słowem czy jest to poprawny szkielet applikacji allegroGl? [source code src="C++" zwin] #include <gl\gl.h> #include <GL\glu.h> #include <allegro.h> #include <alleggl.h>
volatile int iTimer = 0; void TimerFunc() { // Zwiększanie licznika czasu iTimer++; } END_OF_FUNCTION( TimerFunc );
void InitAllegro() { // Inicjalizacja allegro allegro_init(); // Instalowanie AllegroGL: install_allegro_gl(); // Instalowanie klawiatury install_keyboard(); // Instalowanie timera install_timer(); } void InitGL() { // Ustawienia głębi kolorów allegro_gl_set( AGL_COLOR_DEPTH, 32 ); allegro_gl_set( AGL_Z_DEPTH, 8 ); allegro_gl_set( AGL_SUGGEST, AGL_COLOR_DEPTH | AGL_Z_DEPTH ); // Tworzenie okna if ( set_gfx_mode( GFX_OPENGL_FULLSCREEN, 800, 600, 0, 0 ) ) { // Błąd tworzenia okna komunikat allegro_message( "Nie moge utworzyc okna" ); exit( 1 ); } // Ustawienia ekranu glViewport( 0, 0, SCREEN_W, SCREEN_H ); // Umieszczanie matrycy przekształcania, projektowania // Przełacznik matrycy projektowania glMatrixMode( GL_PROJECTION ); glLoadIdentity(); // Define the projection transformation matrix. Notice // that this is using glFrustum instead of gluPerspective: // In order to use this we need to calculate the min and // max x/y range so as to maintain proper aspect ratio: float fMinX = -1.0f, fMaxX = 1.0f, fMinY = -1.0f, fMaxY = 1.0f; float fAspectRatio = SCREEN_W / (float)SCREEN_H; if ( fAspectRatio > 1.0f ) { // The height is greater than the width, so we need to // recalculate the x-range: fMinY = fMinX / fAspectRatio; fMaxY = fMaxX / fAspectRatio; } else { // The width is greater than the height, so we need to // re-calculate the y-range: fMinX = fMinY * fAspectRatio; fMaxX = fMaxY * fAspectRatio; } glFrustum( fMinX, fMaxX, fMinY, fMaxY, 1.5f, 20.0f ); // Przełanczanie matrycy projektowania glMatrixMode( GL_MODELVIEW ); // Kolor tła glClearColor( 0, 0, 0, 0 ); }
void RenderScene() { // Czyszczenie ekranu i bufora głebi glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Renderowanie kwadratu glBegin( GL_QUADS ); glColor3f( 1.0f, 0.0f, 0.0f );// Kolor wierzchołka glVertex3f( -1.0f, -1.0f, 0.0f );//Współrzedne wierzchołka glColor3f( 0.0f, 1.0f, 0.0f ); glVertex3f( 1.0f, -1.0f, 0.0f ); glColor3f( 0.0f, 0.0f, 1.0f ); glVertex3f( 1.0f, 1.0f, 0.0f ); glColor3f( 1.0f, 1.0f, 1.0f ); glVertex3f( -1.0f, 1.0f, 0.0f ); glEnd(); // Oczyszczanie glFlush(); // Przerysowanie ekranu allegro_gl_flip(); } int main() { // Inicjalizacja allegro InitAllegro(); // Inicjalizacja allegrogl InitGL(); LOCK_VARIABLE( iTimer ); LOCK_FUNCTION( TimerFunc ); // Timer wyświetlający 60 klatek na sekunde install_int_ex( TimerFunc, BPS_TO_TIMER( 60 ) ); glLoadIdentity(); glTranslatef( 0.0f, 0.0f, -5.0f );// Przesunięcie sceny w tył float fAngleIncrement = 1.0f / 60.0f * 360.0f; while ( !key[ KEY_ESC ] ) { while(iTimer>0) { // Obracanie sceny glRotatef( fAngleIncrement, 0.0f, 0.0f, 1.0f ); // Wyświetlanie sceny RenderScene(); iTimer--; } } //Zwalnianie zasobów allegro_exit(); remove_allegro_gl(); } END_OF_MAIN(); [/source] |