Created
September 2, 2012 14:21
-
-
Save anonymous/3599450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef _CAPP_H_ | |
| #define _CAPP_H_ | |
| #include <SDL/SDL.h> | |
| #include "Define.h" | |
| #include "CArea.h" | |
| #include "CCamera.h" | |
| #include "CEntity.h" | |
| #include "CEvent.h" | |
| #include "CSurface.h" | |
| #define PI 3.14159265 | |
| //============================================================================== | |
| class CApp : public CEvent { | |
| private: | |
| bool Running; | |
| SDL_Surface* Surf_Display; | |
| public: | |
| CApp(); | |
| int OnExecute(); | |
| public: | |
| bool OnInit(); | |
| void OnEvent(SDL_Event* Event); | |
| void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode); | |
| void OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode); | |
| void OnExit(); | |
| void OnLoop(); | |
| void GetKey(); | |
| void TurnShip(); | |
| void OnRender(); | |
| void OnCleanup(); | |
| public: | |
| double FWDM; | |
| int param; | |
| int CamX, CamY; | |
| }; | |
| //============================================================================== | |
| #endif | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //============================================================================== | |
| #include "CApp.h" | |
| #include <math.h> | |
| #include <iostream> | |
| #include <Windows.h> | |
| int x2; | |
| int y2; | |
| //============================================================================== | |
| void CApp::OnEvent(SDL_Event* Event) { | |
| CEvent::OnEvent(Event); | |
| } | |
| void CApp::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) { | |
| if (FWDM > 0) { | |
| FWDM -= 0.1; | |
| } | |
| } | |
| void CApp::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) { | |
| CApp::GetKey(); | |
| } | |
| //============================================================================== | |
| //------------------------------------------------------------------------------ | |
| void CApp::OnExit() { | |
| Running = false; | |
| } | |
| //============================================================================== | |
| void CApp::GetKey() { | |
| Uint8 *keystate = SDL_GetKeyState(NULL); | |
| if(keystate[SDLK_w]) { | |
| if (FWDM < 6) { | |
| FWDM += 0.2; | |
| } | |
| } | |
| if(keystate[SDLK_s]) { | |
| if (FWDM > 0) { | |
| FWDM -= 0.5; | |
| } | |
| } | |
| if(keystate[SDLK_a]) { | |
| if (param > -1) { | |
| param--; | |
| } | |
| else { | |
| param = 360; | |
| } | |
| } | |
| if(keystate[SDLK_d]) { | |
| if (param < 361) { | |
| param++; | |
| } | |
| else { | |
| param = 0; | |
| } | |
| } | |
| CApp::TurnShip(); | |
| if(keystate[SDLK_ESCAPE]) { | |
| Running = false; | |
| } | |
| } | |
| void CApp::TurnShip() { | |
| int CamX = 0; | |
| int CamY = 0; | |
| x2 = 10 * cos (param*PI/180); | |
| y2 = 10 * sin (param*PI/180); | |
| if (x2 > CamX) { | |
| FWDM -= 0.1; | |
| CamX += 3; | |
| } | |
| if (x2 > CamX) { | |
| CamX -= 3; | |
| FWDM -= 0.1; | |
| } | |
| if (y2 > CamY) { | |
| CamY += 3; | |
| FWDM -= 0.1; | |
| } | |
| if (y2 < CamY) { | |
| CamY -= 3; | |
| FWDM -= 0.1; | |
| } | |
| CCamera::CameraControl.OnMove( CamX, CamY); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //============================================================================== | |
| #include "CApp.h" | |
| using namespace std; | |
| //============================================================================== | |
| bool CApp::OnInit() { | |
| if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { | |
| return false; | |
| } | |
| if((Surf_Display = SDL_SetVideoMode(WWIDTH, WHEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) { | |
| return false; | |
| } | |
| if(CArea::AreaControl.OnLoad("maps/1.area") == false) { | |
| return false; | |
| } | |
| SDL_EnableKeyRepeat(1, SDL_DEFAULT_REPEAT_INTERVAL / 3); | |
| FWDM = 0; | |
| param = 90; | |
| return true; | |
| } | |
| //============================================================================== | |
| //============================================================================== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //============================================================================== | |
| #include "CApp.h" | |
| //============================================================================== | |
| void CApp::OnLoop() { | |
| //-------------------------------------------------------------------------- | |
| // Entities | |
| //-------------------------------------------------------------------------- | |
| for(int i = 0;i < CEntity::EntityList.size();i++) { | |
| if(!CEntity::EntityList[i]) continue; | |
| CEntity::EntityList[i]->OnLoop(); | |
| } | |
| } | |
| //============================================================================== | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //============================================================================== | |
| #include "CApp.h" | |
| //============================================================================== | |
| void CApp::OnRender() { | |
| SDL_Rect Rect; | |
| Rect.x = 0; | |
| Rect.y = 0; | |
| Rect.w = WWIDTH; | |
| Rect.h = WHEIGHT; | |
| SDL_FillRect(Surf_Display, &Rect, 0); | |
| CArea::AreaControl.OnRender(Surf_Display, CCamera::CameraControl.GetX(), CCamera::CameraControl.GetY()); | |
| //-------------------------------------------------------------------------- | |
| // Entities | |
| //-------------------------------------------------------------------------- | |
| for(int i = 0;i < CEntity::EntityList.size();i++) { | |
| if(!CEntity::EntityList[i]) continue; | |
| CEntity::EntityList[i]->OnRender(Surf_Display); | |
| } | |
| SDL_Flip(Surf_Display); | |
| } | |
| //============================================================================== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "CCamera.h" | |
| CCamera CCamera::CameraControl; | |
| CCamera::CCamera() { | |
| X = Y = 40; | |
| TargetX = TargetY = NULL; | |
| TargetMode = TARGET_MODE_NORMAL; | |
| } | |
| void CCamera::OnMove(int MoveX, int MoveY) { | |
| X += MoveX; | |
| Y += MoveY; | |
| } | |
| int CCamera::GetX() { | |
| if(TargetX != NULL) { | |
| if(TargetMode == TARGET_MODE_CENTER) { | |
| return *TargetX - (WWIDTH / 2); | |
| } | |
| return *TargetX; | |
| } | |
| return X; | |
| } | |
| int CCamera::GetY() { | |
| if(TargetY != NULL) { | |
| if(TargetMode == TARGET_MODE_CENTER) { | |
| return *TargetY - (WHEIGHT / 2); | |
| } | |
| return *TargetY; | |
| } | |
| return Y; | |
| } | |
| void CCamera::SetPos(int X, int Y) { | |
| this->X = X; | |
| this->Y = Y; | |
| } | |
| void CCamera::SetTarget(int* X, int* Y) { | |
| TargetX = X; | |
| TargetY = Y; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef _CCAMERA_H_ | |
| #define _CCAMERA_H_ | |
| #include <SDL/SDL.h> | |
| #include "Define.h" | |
| enum { | |
| TARGET_MODE_NORMAL = 0, | |
| TARGET_MODE_CENTER | |
| }; | |
| class CCamera { | |
| public: | |
| static CCamera CameraControl; | |
| private: | |
| int X; | |
| int Y; | |
| int* TargetX; | |
| int* TargetY; | |
| public: | |
| int TargetMode; | |
| public: | |
| CCamera(); | |
| public: | |
| void OnMove(int MoveX, int MoveY); | |
| public: | |
| int GetX(); | |
| int GetY(); | |
| public: | |
| void SetPos(int X, int Y); | |
| void SetTarget(int* X, int* Y); | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment