Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Frozelar's program thingy
- don't steal plz */
- // libraries and external files that will be used
- #include <SDL.h>
- #include <SDL_image.h>
- #include <SDL_mixer.h>
- #include <SDL_ttf.h>
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- // default dimensions
- const int WINDOW_W = 640;
- const int WINDOW_H = 480;
- const int LEVEL_W = 1280;
- const int LEVEL_H = 720;
- // default properties
- const int DEFAULT_H = 64;
- const int DEFAULT_W = 64;
- const int DEFAULT_FRAMES = 4;
- const int DEFAULT_X = 0;
- const int DEFAULT_Y = 0;
- const int SPRITE_SHEET_DEFAULT = 4;
- const int DEFAULT_PLAYER_POS = 11;
- void clearScreen(void);
- void updateScreen(void);
- int touchesThing(SDL_Rect, int**);
- // possible types of events
- enum EventTypes{
- PLAYER_EVENT, MENU_EVENT, WINDOW_EVENT
- };
- // blocks
- const int TOTAL_BLOCKS = 64;
- // contains the sprite sheet
- SDL_Rect *spriteSheet[SPRITE_SHEET_DEFAULT];
- // contains possible sprite types
- enum block_types
- {
- BLANK_BLOCK, SOLID_BLOCK, ENEMY_BLOCK, PLAYER_BLOCK, TOTAL_BLOCK_TYPES
- };
- // enemies
- const int TOTAL_ENEMIES = 4;
- class Camera
- {
- public:
- // initialize everything
- Camera(int x, int y, int w, int h);
- // free and unload everything
- ~Camera();
- // SDL_Rect that represents the camera
- SDL_Rect cmRect;
- };
- class Texture
- {
- public:
- // initialize texture & properties
- Texture(int x, int y, int w, int h);
- // destroy texture, deallocate memory, etc
- ~Texture();
- // load from file
- bool loadF(string path);
- // render to screen
- void render(SDL_Rect *clip, SDL_Point *center, double rotate, SDL_RendererFlip flip);
- // free memory and whatnot
- void free();
- // set color using red, green, blue
- void color(Uint8 r, Uint8 g, Uint8 b);
- // set transparency (aka alpha)
- void alpha(Uint8 a);
- // set blend mode
- void blend(SDL_BlendMode b);
- // if sdl_ttf, load from rendered text
- #ifdef _SDL_TTF_H
- bool loadT(string text, SDL_Color color, TTF_Font* font);
- #endif
- // rect that represents the texture
- SDL_Rect txRect;
- // points to the texture itself
- SDL_Texture* txTexture;
- };
- class Sprite
- {
- public:
- // initialize everything
- Sprite(int x, int y, int w, int h);
- // unload & deinitialize everything
- virtual ~Sprite();
- // moves Sprite and checks collision (variable determines whether we're moving a player or enemy)
- void move(int a);
- // kills the Sprite in-game
- void die();
- // places Sprite in level
- void set();
- // box that represents the Sprite
- SDL_Rect spRect;
- // number of frames
- int spFrames;
- // maximum velocity
- const int spMaxVel = 10;
- // current velocity
- int spXvel;
- int spYvel;
- // the texture that is displayed
- Texture *spTexture;
- };
- class Player:public Sprite
- {
- public:
- // initialize everything associated with the player
- Player(int x, int y, int w, int h);
- // destroy and deallocate everything associated with the player
- ~Player();
- // handles event(s), makes player jump, move, or die
- void handleEvent(SDL_Event &e);
- // center camera over player
- void centerCamera();
- // type
- const Uint8 type = 3;
- };
- class Enemy:public Sprite
- {
- public:
- // initialize everything
- Enemy(int x, int y, int w, int h, int type);
- // unload everything
- ~Enemy();
- // type
- const Uint8 type = 2;
- // type of enemy
- int enType;
- };
- class Background
- {
- public:
- // initialize the background
- Background(int x, int y, int w, int h);
- // unload the background
- ~Background();
- // set up the background
- void set();
- // unloads everything
- void freebg();
- // rect that represents the background
- SDL_Rect bgRect;
- // number of frames
- int bgFrames;
- // points to the background image(s)
- SDL_Rect *bgImages[DEFAULT_FRAMES];
- // background texture
- Texture *bgTexture;
- };
- class Block
- {
- public:
- // initialize everything
- Block(int x, int y, int w, int h, int type);
- // unload everything
- ~Block();
- // type
- const Uint8 type = 1;
- // rect that represents the block
- SDL_Rect blRect;
- // the type of block
- int blType;
- /* CODE FOR BLOCK FRAMES
- // the box that represents the block's frame(s)
- SDL_Rect *cBox[DEFAULT_FRAMES];
- */
- // number of frames
- int blFrames;
- // the texture
- Texture *blTexture;
- };
- class Item
- {
- };
- class Menu
- {
- };
- // contains what should be placed where
- Block *blocks[TOTAL_BLOCKS];
- Enemy *enemies[TOTAL_BLOCKS];
- Player *playerPos[TOTAL_BLOCKS];
- // window and renderer that draws to the window
- SDL_Window *gWindow = NULL;
- SDL_Renderer *gRenderer = NULL;
- // player, camera, and background
- Player gPlayer(0, 0, 64, 64);
- Camera gCamera(0, 0, 64, 64);
- Background gBackground(0, 0, 64, 64);
- Camera::Camera(int passedX = (WINDOW_W / 2 + LEVEL_W / 2), int passedY = (WINDOW_H + LEVEL_H / 2), int passedW = WINDOW_W, int passedH = WINDOW_H)
- {
- // initialize variables
- cmRect.x = passedX;
- cmRect.y = passedY;
- cmRect.w = passedW;
- cmRect.h = passedH;
- }
- Camera::~Camera()
- {
- // UNNECESSARY???
- }
- Texture::Texture(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H)
- {
- // initialize texture pointer & variables
- txTexture = NULL;
- txRect.x = passedX;
- txRect.y = passedY;
- txRect.w = passedW;
- txRect.h = passedH;
- }
- Texture::~Texture()
- {
- // free everything
- free();
- }
- Sprite::Sprite(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H)
- {
- // initialize variables
- spRect.x = passedX;
- spRect.y = passedY;
- spRect.w = passedW;
- spRect.h = passedH;
- spXvel = 0;
- spYvel = 0;
- }
- Sprite::~Sprite()
- {
- // free current texture that spriteTexture points to
- spTexture->free();
- // unload texture
- spTexture = NULL;
- }
- Player::Player(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H)
- {
- // initialize variables
- spRect.x = passedX;
- spRect.y = passedY;
- spRect.w = passedW;
- spRect.h = passedH;
- spFrames = 4;
- // load the player's texture
- spTexture->loadF("resources/cubeman.png");
- // center the camera around the player
- centerCamera();
- }
- Player::~Player()
- {
- // UNNECESSARY??
- }
- Enemy::Enemy(int x = DEFAULT_X, int y = DEFAULT_Y, int w = DEFAULT_W, int h = DEFAULT_H, int pType = NULL)
- {
- // initialize variables
- spRect.x = x;
- spRect.y = y;
- spRect.w = w;
- spRect.h = h;
- enType = pType;
- // load enemy's texture
- spTexture->loadF("resources/enemy.png");
- }
- Enemy::~Enemy()
- {
- // unload the texture
- spTexture = NULL;
- }
- Block::Block(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = DEFAULT_W, int passedH = DEFAULT_H, int type = NULL)
- {
- // initialize variables
- blRect.x = passedX;
- blRect.y = passedY;
- blRect.w = passedW;
- blRect.h = passedH;
- // load texture for block
- blTexture->loadF("resources/block.png");
- }
- Block::~Block()
- {
- // UNNECESSARY???
- }
- Background::Background(int passedX = DEFAULT_X, int passedY = DEFAULT_Y, int passedW = LEVEL_W, int passedH = LEVEL_H)
- {
- // initialize pointer & variables
- bgTexture = NULL;
- bgFrames = 1;
- bgRect.x = passedX;
- bgRect.y = passedY;
- bgRect.w = passedW;
- bgRect.h = passedH;
- // load background
- bgTexture->loadF("resources/bg.png");
- }
- Background::~Background()
- {
- // free up the background
- freebg();
- }
- void Background::freebg()
- {
- // free up background texture
- bgTexture->free();
- // make texture point to NULL
- bgTexture = NULL;
- }
- #ifdef _SDL_TTF_H
- bool Texture::loadT(string text, SDL_Color color, TTF_Font* font)
- {
- // discard existing texture if it exists
- free();
- // stores any failures
- bool success = true;
- // turn text into a surface
- SDL_Surface* textSurface = TTF_RenderText_Solid(font, text.c_str(), color);
- // if surface loaded correctly
- if (textSurface != NULL)
- {
- // create texture from surface
- txTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
- // if texture did not load correctly
- if (txTexture == NULL)
- {
- // output an error
- printf("Error creating texture from text surface. SDL_GetError: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- // store properties
- txRect.w = textSurface->w;
- txRect.h = textSurface->h;
- }
- }
- else
- {
- // output an error
- printf("Error loading text surface. SDL_GetError: %s\n", SDL_GetError());
- success = false;
- }
- // return success status
- return success;
- }
- #endif
- void Texture::render(SDL_Rect *clip = NULL, SDL_Point *center = NULL,
- double rotation = 0.0, SDL_RendererFlip flip = SDL_FLIP_NONE)
- {
- // rect to render to
- SDL_Rect renderRect{ txRect.x, txRect.y, txRect.w, txRect.h };
- // if clip is given, set properties
- if (clip != NULL)
- {
- renderRect.w = clip->w;
- renderRect.h = clip->h;
- }
- // render to the screen
- SDL_RenderCopyEx(gRenderer, txTexture, clip, &renderRect, rotation, center, flip);
- }
- bool Texture::loadF(string path)
- {
- // keeps track of whether a failure occurred
- bool success = true;
- // free the current texture (if it exists)
- free();
- // the texture that we will (hopefully) end up with
- SDL_Texture* createdTexture = NULL;
- // the surface that the texture's image will be loaded onto
- SDL_Surface* imageSurface = IMG_Load(path.c_str());
- if (imageSurface == NULL)
- {
- // output a message if the surface failed to load
- printf("Error loading image at the specified path: %s\n", path);
- printf("IMG_GetError: %s\n", IMG_GetError());
- success = false;
- }
- else
- {
- // set the color key
- SDL_SetColorKey(imageSurface, SDL_TRUE, SDL_MapRGB(imageSurface->format, 0, 255, 255));
- // create the texture itself from the surface
- createdTexture = SDL_CreateTextureFromSurface(gRenderer, imageSurface);
- if (createdTexture == NULL)
- {
- // output an error
- printf("Error creating texture from surface. SDL_GetError: %s\n", SDL_GetError());
- }
- else
- {
- txRect.w = imageSurface->w;
- txRect.h = imageSurface->h;
- }
- }
- // the surface isn't needed anymore
- SDL_FreeSurface(imageSurface);
- // make the texture member point to the newly created texture
- txTexture = createdTexture;
- // return whether everything ran successfully or not
- return success;
- }
- void Texture::free()
- {
- // if a texture exists, get rid of it and reinitialize everything
- if (txTexture != NULL)
- {
- SDL_DestroyTexture(txTexture);
- txTexture = NULL;
- txRect.w = DEFAULT_W;
- txRect.h = DEFAULT_H;
- txRect.x = DEFAULT_X;
- txRect.y = DEFAULT_Y;
- }
- }
- void Texture::color(Uint8 r, Uint8 g, Uint8 b)
- {
- // set colors to the texture
- SDL_SetTextureColorMod(txTexture, r, g, b);
- }
- void Texture::blend(SDL_BlendMode b)
- {
- // set the blend mode
- SDL_SetTextureBlendMode(txTexture, b);
- }
- void Texture::alpha(Uint8 a)
- {
- // set alpha to the texture
- SDL_SetTextureAlphaMod(txTexture, a);
- }
- void Sprite::die()
- {
- // CURRENTLY THIS IS MOSTLY A PLACEHOLDER SO I KNOW I PROGRAMMED EVERYTHING CORRECTLY. ACTUAL DEATH ANIMATIONS AND STUFF COME LATER
- Texture deathTexture;
- spTexture->free();
- deathTexture.loadF("resources/die.png");
- deathTexture.render();
- clearScreen();
- updateScreen();
- }
- void Player::handleEvent(SDL_Event &e)
- {
- // if the user pressed a movement key, change the sprite's velocity accordingly
- if (e.type == SDL_KEYDOWN && e.key.repeat == 0)
- {
- switch (e.key.keysym.sym)
- {
- case SDLK_UP: spYvel -= spMaxVel; break;
- case SDLK_DOWN: spYvel += spMaxVel; break;
- case SDLK_LEFT: spXvel -= spMaxVel; break;
- case SDLK_RIGHT: spXvel += spMaxVel; break;
- }
- }
- // if the user released a movement key, change velocity accordingly
- else if (e.type == SDL_KEYUP && e.key.repeat == 0)
- {
- switch (e.key.keysym.sym)
- {
- case SDLK_UP: spYvel += spMaxVel; break;
- case SDLK_DOWN: spYvel -= spMaxVel; break;
- case SDLK_LEFT: spXvel += spMaxVel; break;
- case SDLK_RIGHT: spXvel -= spMaxVel; break;
- }
- }
- // move the player and center the camera around the player
- move(2);
- centerCamera();
- }
- void Sprite::move(int a)
- {
- // rectangle that will represent where the Sprite is about to move
- SDL_Rect moddedRect;
- // set rect's properties to the sprite that is being checked
- moddedRect.x = spRect.x;
- moddedRect.y = spRect.y;
- moddedRect.w = spRect.w;
- moddedRect.h = spRect.h;
- // represents what specifically was touched
- int *thingsTouched[3];
- // change properties to where the sprite is about to move
- moddedRect.x += spXvel;
- moddedRect.y += spYvel;
- // if where the player is about to move is a block
- if (touchesThing(moddedRect, &*thingsTouched))
- {
- while (true)
- {
- // if the player touches a block in one pixel, move player one pixel and break
- moddedRect.x = spRect.x + 1;
- moddedRect.y = spRect.y + 1;
- if (touchesThing(moddedRect, &*thingsTouched))
- {
- spRect.x = moddedRect.x;
- spRect.y = moddedRect.y;
- break;
- }
- else
- {
- // set sprite's position to where it should move
- spRect.x = moddedRect.x;
- spRect.y = moddedRect.y;
- }
- }
- // if we're working with an enemy and it touched the player, kill the player
- if (thingsTouched[1] > 0 && a == 2)
- {
- gPlayer.die();
- }
- // if we're working with an enemy and it touched another enemy,
- else if (thingsTouched[1] > 0 && a == 1)
- {
- // PUT CODE HERE LATER
- }
- else
- {
- // MIGHT PUT CODE FOR BLOCKS HERE LATER
- }
- // if we're working with the player and the player touched an enemy, kill the player
- if (thingsTouched[2] > 0 && a == 1)
- {
- gPlayer.die();
- }
- else if (thingsTouched[2] > 0 && a == 2)
- {
- // MIGHT PUT CODE FOR MULTIPLE PLAYERS HERE LATER
- }
- else
- {
- // MIGHT PUT CODE HERE LATER?? OR MIGHT NOT BE NECESSARY??
- }
- }
- else
- {
- // move the player
- spRect.x += spXvel;
- spRect.y += spYvel;
- }
- }
- int touchesThing(SDL_Rect a, int **thingsTouched)
- {
- // variables to represent the dimensions of the rect whose collision is being checked
- int topLeftA = NULL, topRightA = NULL;
- int bottomLeftA = NULL, bottomRightA = NULL;
- int topLeftB = NULL, topRightB = NULL;
- int bottomLeftB = NULL, bottomRightB = NULL;
- // represent what specifically was touched
- int blocksTouched = NULL, enemiesTouched = NULL, playerTouched = NULL;
- // set the variables accordingly
- topLeftA = a.x;
- topRightA = a.x + a.w;
- bottomLeftA = a.x + a.h;
- bottomRightA = a.x + a.h + a.w;
- // check everything in the level
- for (int i = 0; i < TOTAL_BLOCKS; i++)
- {
- // if there is a block in that level space by default
- if (blocks[i] != NULL)
- {
- // check its current position to see if a collision occurred
- if ((blocks[i]->blRect.x) <= (a.x) ||
- (blocks[i]->blRect.x + blocks[i]->blRect.w) >= (a.x + a.w) ||
- (blocks[i]->blRect.x + blocks[i]->blRect.h) <= (a.x + a.h) ||
- (blocks[i]->blRect.x + blocks[i]->blRect.h + blocks[i]->blRect.w) >= (a.x + a.h + a.w))
- {
- // if the thing is not colliding with the rect, continue checking
- continue;
- }
- else
- {
- // if the block and rect collided, increase the counter and continue checking
- blocksTouched++;
- }
- }
- // if there is an enemy in that level space by default
- else if (enemies[i] != NULL)
- {
- // check its current position (may not be the initial position) to see if a collision occurred
- if ((enemies[i]->spRect.x) <= (a.x) ||
- (enemies[i]->spRect.x + enemies[i]->spRect.w) >= (a.x + a.w) ||
- (enemies[i]->spRect.x + enemies[i]->spRect.h) <= (a.x + a.h) ||
- (enemies[i]->spRect.x + enemies[i]->spRect.h + enemies[i]->spRect.w) >= (a.x + a.h + a.w))
- {
- // continue if no collision
- continue;
- }
- else
- {
- // increase counter & continue if a collision happened
- enemiesTouched++;
- }
- }
- // if the player starts there by default
- else if (playerPos[i] != NULL)
- {
- // check the player's CURRENT dimensions
- if ((playerPos[i]->spRect.x) <= (a.x) ||
- (playerPos[i]->spRect.x + playerPos[i]->spRect.w) >= (a.x + a.w) ||
- (playerPos[i]->spRect.x + playerPos[i]->spRect.h) <= (a.x + a.h) ||
- (playerPos[i]->spRect.x + playerPos[i]->spRect.h + playerPos[i]->spRect.w) >= (a.x + a.h + a.w))
- {
- // continue if no collision
- continue;
- }
- else
- {
- // increase counter & continue if collision
- playerTouched++;
- }
- }
- }
- // if one or more blocks were touched
- if (blocksTouched > 0)
- {
- // return true and insert amount of blocks in proper array slot
- thingsTouched[0] = &blocksTouched;
- return true;
- }
- // if 1+ enemies were touched
- if (enemiesTouched > 0)
- {
- // return true & insert number of enemies in related slot
- thingsTouched[1] = &enemiesTouched;
- return true;
- }
- // if player was touched
- if (playerTouched > 0)
- {
- // return true & insert that a player was touched
- thingsTouched[2] = &playerTouched;
- return true;
- }
- // return false (no collision occurred)
- return false;
- }
- /* UNUSED FUNCTION, POSSIBLY STILL USEFUL THOUGH
- bool touchesSpecificThing(SDL_Rect a, SDL_Rect b, char type)
- {
- int topLeftA, topRightA;
- int bottomLeftA, bottomRightA;
- int topLeftB, topRightB;
- int bottomLeftB, bottomRightB;
- topLeftA = a.x;
- topRightA = a.x + a.w;
- bottomLeftA = a.x + a.h;
- bottomRightA = a.x + a.h + a.w;
- topLeftB = b.x;
- topRightB = b.x + b.w;
- bottomLeftB = b.x + b.h;
- bottomRightB = b.x + b.h + b.w;
- if (topLeftA <= topLeftB || topRightA >= topRightB ||
- bottomLeftA <= bottomLeftB || bottomRightA >= bottomRightB)
- return false;
- else
- {
- switch (type)
- {
- case 'b':
- {
- // if the player touched a block, return true
- return true;
- }
- case 'e':
- {
- // if the player touched an enemy, kill the player and return true
- gPlayer.die();
- return true;
- }
- }
- }
- }
- */
- void Player::centerCamera()
- {
- // variable to represent the camera
- SDL_Rect* camera = &gCamera.cmRect;
- camera->w = WINDOW_W;
- camera->h = WINDOW_H;
- // determines whether the camera is at the level's edge or not
- bool edge = false;
- // represents the camera when it moves
- SDL_Rect moddedRect;
- // set moddedRect properties
- moddedRect.x = camera->x;
- moddedRect.y = camera->y;
- moddedRect.w = camera->w;
- moddedRect.h = camera->h;
- // set moddedRect to camera's next move
- moddedRect.x++;
- moddedRect.y++;
- // if camera is about to go off the left of the screen, stop its movement
- if (moddedRect.x < camera->w / 2)
- {
- camera->x = 0;
- edge = true;
- }
- // if it is about to go off the right, stop it
- if (moddedRect.x > LEVEL_W - camera->w / 2)
- {
- camera->x = LEVEL_W - camera->w;
- edge = true;
- }
- // if it is about to go off the top, stop it
- if (moddedRect.y > camera->h / 2)
- {
- camera->y = 0;
- edge = true;
- }
- // if it is about to go off the bottom, stop it
- if (moddedRect.y > LEVEL_H - camera->h / 2)
- {
- camera->y = LEVEL_H - camera->h;
- edge = true;
- }
- // if not at the edge of the level, keep camera centered around player
- if (!edge)
- {
- camera->x = spRect.x - (camera->w / 2);
- camera->y = spRect.y - (camera->h / 2);
- }
- }
- bool setBlocks()
- {
- // open block file for reading
- ifstream blockSet("resources/blocks.map");
- // identify type of block from file
- int blockType;
- // current position where a block is being placed
- int x = 0, y = 0;
- // indicates whether a success occurred or not
- bool success = true;
- for (int i = 0; i <= TOTAL_BLOCKS; i++)
- {
- // make each level space equal to NULL so error checking and whatnot is easier
- blocks[i] = NULL;
- enemies[i] = NULL;
- playerPos[i] = NULL;
- }
- /* CODE FOR FRAMES
- int i = 0;
- for (i = 0; i <= DEFAULT_FRAMES; i++)
- {
- cBox[i]->x = x;
- cBox[i]->y = y;
- cBox[i]->w = w;
- cBox[i]->h = h;
- }
- */
- /* I thought this was supposed to work but I get an error so I dunno
- if (blockSet == NULL)
- {
- // if loading the level failed, output an error
- printf("Error loading level file!\n");
- success = false;
- }
- else
- {
- */
- for (int i = 0; i <= TOTAL_BLOCKS; i++)
- {
- // initialize value to -1
- blockType = -1;
- // insert the type of block into the variable (0-3)
- blockSet >> blockType;
- if (blockSet.fail())
- {
- // if reading the file failed, output an error
- printf("Error reading level file!\n");
- success = false;
- }
- else
- {
- if (blockType == 1)
- {
- // place a block in this level space
- blocks[i] = new Block(x, y);
- }
- else if (blockType == 0)
- {
- // place nothing in this level space
- continue;
- }
- else if (blockType == 2)
- {
- // place an enemy in this level space
- enemies[i] = new Enemy(x, y);
- }
- else if (blockType == 3)
- {
- // place the player's start in this level space
- playerPos[i] = new Player(x, y);
- }
- else
- {
- // the block type is not valid
- printf("ERROR: Invalid block type: %d\n", blockType);
- return false;
- }
- if (x + DEFAULT_W >= LEVEL_W)
- {
- // if at level edge, go back to the left and move down one space
- x = 0;
- y += DEFAULT_H;
- }
- else
- {
- // move to the right one space
- x += DEFAULT_W;
- }
- }
- }
- // load sprite sheet if loaded correctly
- if (success)
- {
- // set the spritesheet array equal to the sprites' positions on the actual spritesheet
- spriteSheet[0]->x = DEFAULT_X;
- spriteSheet[0]->y = DEFAULT_Y;
- spriteSheet[0]->w = DEFAULT_W;
- spriteSheet[0]->h = DEFAULT_H;
- spriteSheet[1]->x = DEFAULT_X + DEFAULT_W;
- spriteSheet[1]->y = DEFAULT_Y;
- spriteSheet[1]->w = DEFAULT_W;
- spriteSheet[1]->h = DEFAULT_H;
- spriteSheet[2]->x = DEFAULT_X + (DEFAULT_W * 2);
- spriteSheet[2]->y = DEFAULT_Y;
- spriteSheet[2]->w = DEFAULT_W;
- spriteSheet[2]->h = DEFAULT_H;
- spriteSheet[3]->x = DEFAULT_X + (DEFAULT_W * 3);
- spriteSheet[3]->y = DEFAULT_Y;
- spriteSheet[3]->w = DEFAULT_W;
- spriteSheet[3]->h = DEFAULT_H;
- }
- /*
- }
- */
- // close the file
- blockSet.close();
- // return whether loading the level was a success or not
- return success;
- }
- bool init()
- {
- // keeps track of whether or not an error occurred
- bool success;
- // initialize video; if failed, output an error & set success to false
- if (SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- printf("SDL initialization error: %s\n", SDL_GetError());
- success = false;
- }
- else
- {
- // make texture filtering linear; if failed, output a notification
- if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
- {
- printf("NOTE: Hint failed to be set!");
- }
- // create the window and everything
- gWindow = SDL_CreateWindow("Oh man I made a program", SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED, WINDOW_W, WINDOW_H, SDL_WINDOW_SHOWN);
- // if window creation failed, output an error
- if (gWindow == NULL)
- {
- printf("SDL window creation error: %s\n", SDL_GetError());
- success = false;
- }
- // if no error occurred
- else
- {
- // create a renderer for use later
- gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- // if the renderer failed to be created, output an error & set success to false
- if (gRenderer == NULL)
- {
- printf("SDL renderer creation error: %s\n", SDL_GetError());
- success = false;
- }
- // if no error occurred
- else
- {
- // initialize image loading; if failed, output an error & set success to false
- if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
- {
- printf("SDL_image initialization error: %s\n", IMG_GetError());
- success = false;
- }
- }
- }
- }
- // return the success status
- return success;
- }
- void clearScreen()
- {
- // sets screen color
- SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
- // set screen up for updating
- SDL_RenderClear(gRenderer);
- }
- void updateScreen()
- {
- // update the screen
- SDL_RenderPresent(gRenderer);
- }
- void close()
- {
- // delete each block, enemy, and player from the level
- for (int i = 0; i <= TOTAL_BLOCKS; i++)
- {
- if (blocks[i] != NULL)
- delete blocks[i];
- else if (enemies[i] != NULL)
- delete enemies[i];
- else if (playerPos[i] != NULL)
- delete playerPos[i];
- }
- }
- int main(int argc, char *argv[])
- {
- // texture pointer to make things easier later on
- Texture* pTexture;
- // keeps track of if the user requests to quit
- bool quit = false;
- // keeps track of what the user inputs
- SDL_Event e;
- // output an error if initialization was not successful
- if (!init())
- {
- printf("ERROR: SDL failed to initialize.");
- }
- else
- {
- // output an error if setting up the level blocks failed
- if (!setBlocks())
- {
- printf("ERROR: Could not load level.");
- }
- // BY THIS POINT, ALL BLOCKS/ENEMIES/PLAYER SHOULD BE SET
- else
- {
- // while the user does not want to quit
- while (!quit)
- {
- // while the user requests something
- while (SDL_PollEvent(&e) != NULL)
- {
- // if they requested to quit, set the quit boolean to true
- if (e.key.keysym.sym == SDL_QUIT)
- {
- quit = true;
- }
- // handle the event if it wasn't a request to quit
- gPlayer.handleEvent(e);
- }
- }
- // re-render the player texture
- pTexture = gPlayer.spTexture;
- pTexture->render();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement