Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //I fixed my original code in this paste
- /*
- I am still learning how to do a few things and game loops themselves. Now that I got this mostly working I will be starting fresh with what I learned with this one. My TODO list will carry over and extend, as well.
- */
- //main.cpp
- #include "CEvent.h"
- int main ( int argc, char* argv[] )
- {
- CEvent event;
- event.Load();
- event.Init();
- while(event.State())
- {
- event.Input();
- event.Draw();
- event.Update();
- }
- event.Cleanup();
- return 0;
- }
- //TODO: Put pictures on a working dialog -DONE
- //TODO: Make picture move -DONE
- //TODO: Make sprite disappear an reappear on command -DONE
- //TODO: Make invisible sprite reappear at 0,0 after reappearance -IN PROGRESS
- //TODO: Addendum: Keep sprite in dialog -DONE
- //TODO: Eat food -DONE COUNTLESS TIMES
- //TODO: Take asprin -AVOIDED FOR NOW...BARELY
- //TODO: Figure out that most alien of concepts(Game Loop) -IN PROGRESS
- //TODO: Add collision with other objects -IN PROGRESS (Considering Asprin)
- //TODO: Make fancy lasers that start at one sprite and end at another -NOT STARTED
- //TODO: Make laser hurt a sprite -NOT STARTED
- //TODO: Think of more to do -PREFER NOT TO NEED ASPRIN YET
- /*//////////////////////////////////////////////////////////////////////////////////////*/
- CEvent.h
- #include "GameData.h"
- class CEvent
- {
- public:
- void Load();
- void Init();
- void Input();
- void Draw();
- void Update();
- void Cleanup();
- bool State();
- //I have no clue as to wether or not these next 3 are in the correct location
- void Apply_Surface(int x, int y, SDL_Surface * source, SDL_Surface * destination);
- void Draw_Sprite (int srcx, int srcy, int dstx, int dsty, int width, int height, SDL_Surface *source, SDL_Surface *destination);
- void Collision(int Vertical_Axis_1, int Horizontal_Axis_1, int Vertical_Axis_2, int Horizontal_Axis_2);
- private:
- SDL_Surface *screen;
- bool running;
- bool Collided;
- };
- void CEvent::Load()
- {
- //It said load...so I loaded sprites: I consider the background to be a sprite
- background = SDL_LoadBMP("starfield.bmp");
- sprite = SDL_LoadBMP("ship1.bmp");
- //SDL_Init(SDL_INIT_EVERYTHING);
- }
- void CEvent::Init()
- {
- running = true;
- //It has INIT in it...you guessed it. I put it in Init()
- SDL_Init(SDL_INIT_EVERYTHING);
- //I figured the main window should be an important part of the INIT process
- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_NOFRAME);
- //Goes with the window, in my confused mind
- SDL_WM_SetCaption("YouTube Tut 01", NULL);
- Player_Exists = true;
- }
- void CEvent::Input()
- {
- if(SDL_PollEvent(&event))
- {
- //The scary red x
- if(event.type == SDL_QUIT)
- {
- running = false;
- }
- if(event.type == SDL_KEYDOWN)
- {
- keys[event.key.keysym.sym] = true;
- }
- if(event.type == SDL_KEYUP)
- {
- keys[event.key.keysym.sym] = false;
- }
- }
- //using key to exit rather than the red x. I dislike using the red x
- if(keys[SDLK_q])
- {
- running = false;
- }
- //..........................Fun with keys..........................
- //Put center of sprite in center of screen. I couldn't believe it worked perfectly on my first attempt. Woo! Hoo!
- if(keys[SDLK_1])
- {
- dsty = ((SCREEN_HEIGHT/2) - (PLAYER_HEIGHT/2));
- dstx = ((SCREEN_WIDTH/2) - (PLAYER_WIDTH/2));
- }
- //Trying and failing to remove sprite: Game crashes, unfortunatly
- if(keys[SDLK_5])
- {
- Player_Exists = false;
- }
- if(keys[SDLK_6])
- {
- Player_Exists = true;
- }
- if(keys[SDLK_0])
- {
- dsty = 0;
- dstx = 0;
- }
- if(keys[SDLK_HOME])
- {
- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_NOFRAME);
- }
- if(keys[SDLK_END])
- {
- screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_NOFRAME | SDL_FULLSCREEN);
- }
- if(keys[SDLK_UP])
- {
- if(dsty > 1)
- {
- dsty -= 5;
- }
- }
- if(keys[SDLK_DOWN])
- {
- //Still has an empty pixel on the bottom. Is it SDL's fault? Probably not.
- if(dsty < (SCREEN_HEIGHT - (PLAYER_HEIGHT/* + 1*/)))
- {
- dsty += 5;
- }
- }
- if(keys[SDLK_LEFT])
- {
- if(dstx > 0)
- {
- dstx -= 5;
- }
- }
- if(keys[SDLK_RIGHT])
- {
- //Same empty pixel problem as SLDK_DOWN
- if(dstx < (SCREEN_WIDTH - (PLAYER_WIDTH/* + 1*/)))
- {
- dstx += 5;
- }
- }
- }
- void CEvent::Draw()
- {
- Apply_Surface(0, 0, background, screen);
- //if(Player_Exists)
- //{
- Draw_Sprite(srcx, srcy, dstx, dsty, PLAYER_WIDTH, PLAYER_HEIGHT, sprite, screen);
- //}
- /*if(!Player_Exists)
- {
- SDL_FreeSurface(sprite);
- }*/
- }
- void CEvent::Update()
- {
- SDL_Flip(screen);
- }
- void CEvent::Cleanup()
- {
- SDL_FreeSurface(sprite);
- SDL_FreeSurface(background);
- SDL_Quit();
- exit(0);
- }
- bool CEvent::State()
- {
- return running;
- }
- void CEvent::Apply_Surface(int x, int y, SDL_Surface * source, SDL_Surface * destination)
- {
- SDL_Rect rect;
- rect.x = x;
- rect.y = y;
- SDL_BlitSurface(source, NULL, destination, &rect);
- }
- void CEvent::Draw_Sprite (int srcx, int srcy, int dstx, int dsty, int width, int height, SDL_Surface *source, SDL_Surface *destination)
- {
- if(Player_Exists)
- {
- SDL_SetColorKey(source, SDL_SRCCOLORKEY, SDL_MapRGB(source->format, 255, 255, 255));
- SDL_Rect src;
- src.x = srcx;
- src.y = srcy;
- src.w = width;
- src.h = height;
- SDL_Rect dst;
- dst.x = dstx;
- dst.y = dsty;
- dst.w = width;
- dst.h = height;
- SDL_BlitSurface(source, &src, destination, &dst);
- }
- }
- //This function is a work in progress - Tread with care
- void CEvent::Collision(int Vertical_Axis_1, int Horizontal_Axis_1, int Vertical_Axis_2, int Horizontal_Axis_2)
- {
- if(Vertical_Axis_1 >= Vertical_Axis_2 && (Vertical_Axis_1 + PLAYER_WIDTH) <= (Vertical_Axis_2 + PLAYER_WIDTH))
- {
- if(Horizontal_Axis_1 >= Horizontal_Axis_2 && (Horizontal_Axis_1 + PLAYER_HEIGHT) <= (Horizontal_Axis_2 + PLAYER_HEIGHT))
- {
- Collided = true;
- }
- }
- }
- /*////////////////////////////////////////////////////////////////////////////////////////////*/
- GameData.h
- #include <SDL.h>
- //Good enough for now...Like a little control panel
- int SCREEN_WIDTH = 800;//For debugging purposes
- int SCREEN_HEIGHT = 600;//See above
- int BPP = 32;//Seems like a good number
- int PLAYER_WIDTH = 20;//Size of sprite in use
- int PLAYER_HEIGHT = 20;//See above
- int srcx = 0;//12;
- int srcy = 0;//12;
- int dstx = 0;
- int dsty = 0;
- bool keys[323] = {false};//Done by a tutorial creator. Yes, I'm clueless
- SDL_Event event;//Seems like a popular name
- SDL_Surface *background = NULL;//Gotta have some flavor: Placeholder
- SDL_Surface *sprite = NULL;//A two minute toothless space fighter: Placeholder
- bool Player_Exists;
- /*///////////////////////////////////////////////////////////////////////////////////////////*/
- CCollisionDetection.h
- #ifndef CCOLLISIONDETECTION_H
- #define CCOLLISIONDETECTION_H
- //Not started yet
- class CCollisionDetection
- {
- public:
- CCollisionDetection();
- virtual ~CCollisionDetection();
- protected:
- private:
- };
- #endif // CCOLLISIONDETECTION_H
- /*///////////////////////////////////////////////////////////////////////////////////////////*/
- CCollisionDetection.cpp
- #include "CCollisionDetection.h"
- //Not Started yet
- CCollisionDetection::CCollisionDetection()
- {
- //ctor
- }
- CCollisionDetection::~CCollisionDetection()
- {
- //dtor
- }
Advertisement
Add Comment
Please, Sign In to add comment