Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rwxr-xr-xsrc/main.cpp383
1 files changed, 383 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100755
index 0000000..a90b08e
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,383 @@
+//--------------------------------------------------------------------------------------------------
+// Arcade game for the XO computer (OLPC).
+// by CeibalJAM.
+//
+// NOTE: This was made quickly with the purpose of testing the performance of the XO machine.
+// The code is not optimized and it should be worked more in order to be used in a production environment.
+// Please use it for learning purposes and/or as a starting point.
+//--------------------------------------------------------------------------------------------------
+
+// TODO:
+// - Convert the CFont routines to a class CFont.
+// - Add functions in CFont to draw in a CSurface object.
+
+#include <SDL.h>
+#include <SDL_mixer.h>
+#include <stdio.h>
+#include "CBenchmark.h"
+#include "CFont.h"
+#include "CGame.h"
+#include "CGameConstants.h"
+#include "CPlayer.h"
+#include "CSurface.h"
+#include "CSpriteAnimation.h"
+#include "CSprite.h"
+#include "CTimer.h"
+
+//The music that will be played
+//Mix_Music *music = NULL;
+
+//The sound effects that will be used
+//Mix_Chunk *sfx1 = NULL;
+//Mix_Chunk *sfx2 = NULL;
+//Mix_Chunk *sfx3 = NULL;
+//Mix_Chunk *sfx4 = NULL;
+
+// Screen surface.
+SDL_Surface *mScreen = NULL;
+CSurface *mScreenSurface = NULL;
+
+//SDL_Surface *mImageBMP = NULL;
+//SDL_Surface *mImagePNG = NULL;
+//CSurface *mImageBMP2 = NULL;
+//CSurface *mImagePNG2 = NULL;
+
+
+
+CBenchmark *mBenchmark;
+
+char *string = "Xoxi y los Derechos del Nino";
+
+char *string2 = "Press 1, 2, 3, or 4 to play a sound effect";
+char *string3 = "Press 9 to play or pause the music";
+
+SDLFont *font1;
+SDLFont *font2;
+
+CGame *mGame;
+
+bool init()
+{
+ // SDL_Init is the first function of SDL we have to call.
+ // Initialize video, audio and joystick subsystems.
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == -1)
+ {
+ fprintf(stderr, "SDL initialization failed!\n");
+ fprintf(stderr, "Error: %s\n", SDL_GetError());
+ return false;
+ }
+ else
+ {
+ fprintf(stdout, "SDL initialized properly!\n");
+ // When the program terminates (either normally or by calling the exit function), it will call each of the
+ // functions added to the list of functions sent by atexit in the reverse order of how they were sent.
+ // The function passed as a parameter to atexit() must not have arguments.
+ atexit(SDL_Quit);
+ }
+
+ //Initialize SDL_mixer
+ if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 2048/*4096*/ ) == -1 )
+ {
+ return false;
+ }
+
+ // Set the icon image as the icon of the program.
+ SDL_WM_SetIcon(SDL_LoadBMP("assets/images/icon/icon.bmp"), NULL);
+
+ mScreen = SDL_SetVideoMode(CGameConstants::SCREEN_WIDTH, CGameConstants::SCREEN_HEIGHT, CGameConstants::SCREEN_BPP, SDL_ANYFORMAT);
+ if (mScreen == NULL)
+ {
+ fprintf(stderr, "Could not create screen surface (set video mode)!\n");
+ fprintf(stderr, "Error: %s\n", SDL_GetError());
+ return false;
+ }
+
+ mScreenSurface = new CSurface(mScreen);
+
+ // Set the window caption .
+ SDL_WM_SetCaption( "GameXO", NULL );
+
+ return true;
+}
+
+bool loadImages()
+{
+// mImageBMP = CSurface::loadImage("assets\\images\\andy\\andy.bmp");
+// if (mImageBMP == NULL)
+// return false;
+// mImagePNG = CSurface::loadImage("assets\\images\\andy\\andy.png");
+// if (mImagePNG == NULL)
+// return false;
+//
+// mImageBMP2 = new CSurface("assets\\images\\andy\\andy.bmp");
+// if (mImageBMP2->getImage() == NULL)
+// return false;
+// mImagePNG2 = new CSurface("assets\\images\\andy\\andy.png");
+// if (mImagePNG2->getImage() == NULL)
+// return false;
+
+
+ // Load in the fonts.
+ font1 = initFont("assets/fonts/font1", 1, 0, 0);
+ if (font1 == NULL)
+ {
+ fprintf(stderr, "Could not load font1!\n");
+ exit(1);
+ }
+ font2 = initFont("assets/fonts/font2", 1, 1, 0);
+ if (font2 == NULL)
+ {
+ fprintf(stderr, "Could not load font2!\n");
+ exit(1);
+ }
+
+
+
+
+ return true;
+}
+
+void destroy()
+{
+ delete mGame;
+ mGame = NULL;
+
+ freeFont(font1);
+ freeFont(font2);
+
+
+ // Free the surfaces.
+// SDL_FreeSurface(mImageBMP);
+// SDL_FreeSurface(mImagePNG);
+
+// mImageBMP2->destroy();
+// mImageBMP2 = NULL;
+// mImagePNG2->destroy();
+// mImagePNG2 = NULL;
+
+
+
+ delete mBenchmark;
+ mBenchmark = NULL;
+
+ //Free the sound effects
+/* Mix_FreeChunk( sfx1 );
+ Mix_FreeChunk( sfx2 );
+ Mix_FreeChunk( sfx3 );
+ Mix_FreeChunk( sfx4 );
+
+ //Free the sound effects
+ Mix_FreeMusic( music );
+*/
+ //Quit SDL. The screen surface is deleted here.
+ SDL_Quit();
+}
+
+void update(void)
+{
+ mGame->update();
+}
+
+void render(void)
+{
+ mGame->render();
+
+
+ // CSurface::drawImage(mScreen, mImageBMP, 0, 0);
+ // CSurface::drawImage(mScreen, mImagePNG, 50, 50);
+
+// mImageBMP2->draw(mScreen, 100, 100);
+// mImagePNG2->draw(mScreen, 150, 150);
+
+ // TEST DE QUE NO HAY TRANSPARENCIA !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ //CSurface *t = new CSurface("assets\\images\\andy\\andy_animation.png");
+ //t->draw(mScreen, 100, 100);
+ //delete t;
+ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
+
+// CSurface::drawImage(mScreenSurface, mAndyAnimation->getFrame(0)->getImage(), 400, 400);
+// CSurface::drawImage(mScreen, mAndyAnimation->getFrame(1)->getImage()->getImage(), 300, 100);
+
+
+ if (mBenchmark->getVisible())
+ {
+ CSurface::drawRect(mScreen, 290, 0, 130, 30, 0x000000);
+ drawString(mScreen, font1, 300, 0, "FPS: %d\n", mBenchmark->getDisplayCounter());
+ }
+
+ // Draw the text to the center of the screen.
+ drawString(mScreen,font2,600-stringWidth(font2,string)/2,50,string);
+
+ // Sourn help.
+ drawString(mScreen,font2,600-stringWidth(font2,string2)/2,100,string2);
+ drawString(mScreen,font2,600-stringWidth(font2,string3)/2,150,string3);
+
+ if (SDL_Flip(mScreen) == -1)
+ {
+ fprintf(stderr, "SDL_Flip() failed!\n");
+ fprintf(stderr, "Error: %s\n", SDL_GetError());
+ }
+}
+
+int main(int argc, char* argv[])
+{
+ bool mQuit = false;
+ SDL_Event mEvent;
+
+ mBenchmark = new CBenchmark(true);
+
+ CTimer timer;
+
+ if (!init())
+ {
+ exit(1);
+ }
+
+ if (!loadImages())
+ {
+ exit(2);
+ }
+
+
+ //Load the music
+ //music = Mix_LoadMUS( "assets/audio/music_adventure.ogg" );
+
+ //If there was a problem loading the music
+ //if( music == NULL )
+ //{
+ // fprintf(stderr, "Load music failed!\n");
+ // return false;
+ //}
+
+ //Load the sound effects
+ //sfx1 = Mix_LoadWAV( "assets/audio/sfx1.ogg" );
+ //sfx2 = Mix_LoadWAV( "assets/audio/sfx2.ogg" );
+ //sfx3 = Mix_LoadWAV( "assets/audio/sfx3.ogg" );
+ //sfx4 = Mix_LoadWAV( "assets/audio/sfx4.ogg" );
+
+ //If there was a problem loading the sound effects
+ //if( ( sfx1 == NULL ) || ( sfx2 == NULL ) || ( sfx3 == NULL ) || ( sfx4 == NULL ) )
+ //{
+ // fprintf(stderr, "Load sfx failed!\n");
+ // return false;
+ //}
+
+
+ mGame = new CGame(mScreenSurface);
+
+ mGame->drawMap();
+ render();
+
+ // While the user hasn't quit...
+ while (!mQuit)
+ {
+ float dt = timer.getTimeElapsed();
+ mBenchmark->update(dt);
+
+ // While there's an event to handle.
+ while (SDL_PollEvent(&mEvent))
+ {
+ if (mEvent.type == SDL_QUIT)
+ {
+ fprintf(stdout, "Quit event has occurred.\n");
+ mQuit = true;
+ }
+
+ if (mEvent.type == SDL_KEYDOWN)
+ {
+ if (mEvent.key.keysym.sym == SDLK_ESCAPE )
+ {
+ mQuit = true;
+ }
+ //If 1 was pressed
+/* else if ( mEvent.key.keysym.sym == SDLK_1 )
+ {
+ //Play the scratch effect
+ if( Mix_PlayChannel( -1, sfx1, 0 ) == -1 )
+ {
+ fprintf(stderr, "sfx1 no se puede tocar!\n");
+ //return 1;
+ }
+ }
+ //If 2 was pressed
+ else if( mEvent.key.keysym.sym == SDLK_2 )
+ {
+ //Play the high hit effect
+ if( Mix_PlayChannel( -1, sfx2, 0 ) == -1 )
+ {
+ fprintf(stderr, "sfx2 no se puede tocar!\n");
+ //return 1;
+ }
+ }
+ //If 3 was pressed
+ else if( mEvent.key.keysym.sym == SDLK_3 )
+ {
+ //Play the medium hit effect
+ if( Mix_PlayChannel( -1, sfx3, 0 ) == -1 )
+ {
+ fprintf(stderr, "sfx3 no se puede tocar!\n");
+ //return 1;
+ }
+ }
+ //If 4 was pressed
+ else if( mEvent.key.keysym.sym == SDLK_4 )
+ {
+ //Play the low hit effect
+ if( Mix_PlayChannel( -1, sfx4, 0 ) == -1 )
+ {
+ fprintf(stderr, "sfx4 no se puede tocar!\n");
+ //return 1;
+ }
+ }
+ //If 9 was pressed
+ else if( mEvent.key.keysym.sym == SDLK_9 )
+ {
+ //If there is no music playing
+ if( Mix_PlayingMusic() == 0 )
+ {
+ //Play the music
+ if( Mix_PlayMusic( music, -1 ) == -1 )
+ {
+ fprintf(stderr, "music no se puede tocar!\n");
+ //return 1;
+ }
+ }
+ //If music is being played
+ else
+ {
+ //If the music is paused
+ if( Mix_PausedMusic() == 1 )
+ {
+ //Resume the music
+ Mix_ResumeMusic();
+ }
+ //If the music is playing
+ else
+ {
+ //Pause the music
+ Mix_PauseMusic();
+ }
+ }
+ }
+ //If 0 was pressed4
+ else if( mEvent.key.keysym.sym == SDLK_0 )
+ {
+ //Stop the music
+ Mix_HaltMusic();
+ }
+*/
+ }
+ }
+
+ update();
+ render();
+ }
+
+ destroy();
+
+ fprintf(stdout, "Terminating program normally.\n");
+
+ return 0;
+}
+
+