Pushok

My first SDL game using classes :3

May 9th, 2011
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include "SDLEntity.h"
  4. #include "SDLApplication.h"
  5. #include "SDLSound.h"
  6. #include "SDLTimer.h"
  7. #include "SDLTyper.h"
  8. #include "GameScore.h"
  9.  
  10. int main(int argc, char* argv[])
  11. {
  12.     try{
  13.         SDL_Application *g_Application = new SDL_Application();
  14.         SDLSound *g_Sound = new SDLSound();
  15.         SDLEntity *g_Entity = new SDLEntity();
  16.         SDL_Timer *g_Timer = new SDL_Timer();
  17.         SDLTyper *g_Typer = new SDLTyper();
  18.         GScore *g_Score = new GScore();
  19.  
  20.         if(g_Application->Init() || g_Sound->Init() || g_Typer->Init())
  21.         {
  22.             throw g_Application->InvalidPath;
  23.         }
  24.  
  25.         g_Typer->Init();
  26.         g_Typer->OpenFont(FONT);
  27.  
  28.         g_Application->SetCaption(WINNAME);
  29.         g_Application->InitBackground(BACKGROUND);
  30.         g_Application->DrawBackground();
  31.  
  32.         g_Sound->InitBGSound(BGSOUND);
  33.         g_Sound->EmitBGSound();
  34.         g_Sound->InitActionSound(g_Sound->GoodShoot, SHOOT);
  35.  
  36.         g_Entity->InitSurface(ENTITY);
  37.         g_Entity->Move(g_Application->GetScreenSurface(), 0, 0);
  38.         g_Timer->Set(1000);
  39.  
  40.         while(g_Application->m_isGameRunning)
  41.         {
  42.             g_Application->CheckForGameExit();
  43.             if(g_Application->HandleMouseInput(g_Entity->GetRect()) || !g_Timer->IsTimerRun())
  44.             {
  45.                 g_Application->DrawBackground();
  46.                 g_Entity->Move(g_Application->GetScreenSurface(), rand() % (g_Application->Width - g_Entity->GetRect()->w), rand() % (g_Application->Heigth - g_Entity->GetRect()->h));
  47.                 if(g_Timer->IsTimerRun())
  48.                 {
  49.                     g_Sound->EmitActionSound(g_Sound->GoodShoot);
  50.                     g_Typer->DisplayText(g_Application->GetScreenSurface(), 0, "Nice Shoot!");
  51.                     g_Score->Add();
  52.                 }
  53.                 else
  54.                 {
  55.                     g_Score->Sub();
  56.                     g_Typer->DisplayText(g_Application->GetScreenSurface(), 0, "Fail!");
  57.                 }
  58.                 g_Timer->Set(rand() % (1000 - 500) + 500);
  59.             }
  60.  
  61.             g_Typer->DisplayText(g_Application->GetScreenSurface(), 620, g_Score->ToString().c_str());
  62.             g_Timer->Update();
  63.             g_Application->UpdateWindow();
  64.         }
  65.  
  66.         delete g_Score;
  67.         delete g_Typer;
  68.         delete g_Sound;
  69.         delete g_Application;
  70.         delete g_Entity;
  71.         delete g_Timer;
  72.     }
  73.  
  74.     catch(unsigned exeption)
  75.     {
  76.         char* ErrorMessage = nullptr;
  77.  
  78.         switch(exeption)
  79.         {
  80.         case SDL_Application::InvalidPath:
  81.             {
  82.                 ErrorMessage = "Invalid path.";
  83.                 break;
  84.             }
  85.         case SDL_Application::InvalidEntityId:
  86.             {
  87.                 ErrorMessage = "Invalid entity id.";
  88.                 break;
  89.             }
  90.         case SDL_Application::InvalidSound:
  91.             {
  92.                 ErrorMessage = "Invalid sound.";
  93.                 break;
  94.             }
  95.         }
  96.  
  97.         MessageBoxA(nullptr, (std::string("Game was aborted with error: ") + ErrorMessage).c_str() , nullptr, MB_OK);
  98.     }
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment