Skip to content

Commit

Permalink
Add a simple character with refresh rates
Browse files Browse the repository at this point in the history
  • Loading branch information
arbeiter committed Mar 24, 2022
1 parent 01034d3 commit 19b7f7a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 57 deletions.
48 changes: 7 additions & 41 deletions code/clonegame/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ int SCREEN_HEIGHT = 768;
std::vector<std::tuple<int, int>> brick_positions;

bool Game::initialize() {
mPlayer.width = 50;
mPlayer.height = 40;
TextureManager tempTexManager = TextureManager(mRenderer);
// Initialize SDL
mIsRunning = true;
Expand Down Expand Up @@ -190,7 +192,10 @@ void Game::generateOutput()

void Game::DrawActor() {
TextureManager texManager = TextureManager(mRenderer);
mPlayer.sampleFromSpritesheet(&texManager, spriteSheet, FRAME_NUMBER % 4);
int num_frames = 8;
int delayPerFrame = 500;
int frame = (SDL_GetTicks() / delayPerFrame) % num_frames;
mPlayer.sampleFromSpritesheet(&texManager, spriteSheet, frame);
}


Expand All @@ -216,7 +221,7 @@ void Game::FillScreenWithGrass() {
}

void Game::WallLayer() {
int rr[11][16] = {{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0},
int rr[11][16] = {{0,0,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1},
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0},
{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0},
Expand Down Expand Up @@ -267,58 +272,19 @@ bool Game::print_stats(int brick_x, int brick_y, int player_x, int player_y) {
}
*/

// a -> brick, b -> player
bool Game::get_intersection(int brick_x, int brick_y, int player_x, int player_y) {
int player_width = mPlayer.width;
int player_height = mPlayer.height;
int brick_width = rect_width;
int brick_height = rect_height;

if(player_x + player_width < brick_x || player_x > brick_x + brick_width) {
// std::cout << "X OUTSIDE" << std::endl;
return false;
}

if(player_y > brick_y + brick_height || player_y + player_height < brick_y) {
if(player_y > brick_y + brick_height) {
// std::cout << "PLAYER_Y > BRICK_Y + BRICK_H" << endl;
}
if(player_y + player_height < brick_y) {
// std::cout << "PLAYER_Y + PLAYER_H < BRICK_Y" << endl;
}
/*
if(player_y > brick_y + brick_height) {
std::cout << "PLAYER_Y > BRICK_Y + BRICK_H" << endl;
}
if(player_y + player_height < brick_y) {
std::cout << "PLAYER_Y + PLAYER_H < BRICK_Y" << endl;
}
std::cout << "PLAYER XY "<< player_x << " " << player_y << "BRICKXY" << brick_x << " " << brick_y << std::endl;
std::cout << "PLAYERWIDTH " << player_width << "PLAYERWIDTH" << player_height << "BRICKWIDTH " << brick_width << "BRICKHEIGHT " << brick_height << std::endl;
std::cout << "BRICK HEIGHT SUM" << brick_y + brick_height << "PLAYER HEIGHT SUM" << player_y + player_height << std::endl;
std::cout << "SUCKS Y" << endl;
*/
return false;
}

// cout << "Intersection" << endl;
/*
std::cout << "PLAYER XY "<< player_x << " " << player_y << "BRICKXY" << brick_x << " " << brick_y << std::endl;
std::cout << "PLAYERWIDTH " << player_width << "PLAYERWIDTH" << player_height << "BRICKWIDTH " << brick_width << "BRICKHEIGHT " << brick_height << std::endl;
std::cout << "BRICK HEIGHT SUM" << brick_y + brick_height << "PLAYER HEIGHT SUM" << player_y + player_height << std::endl;
*/
return true;
}

SDL_Texture* Game::LoadTexture(const char* texture)
{
SDL_Surface* tempSurface = IMG_Load(texture);
SDL_Texture* tex = SDL_CreateTextureFromSurface(mRenderer, tempSurface);
SDL_FreeSurface(tempSurface);
return tex;
}

void Game::Draw(SDL_Texture * tex, SDL_Rect src, SDL_Rect dest, SDL_RendererFlip flip)
{
SDL_RenderCopyEx(mRenderer, tex, &src, &dest, NULL, NULL, flip);
}
16 changes: 2 additions & 14 deletions code/clonegame/player.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
#include "player.h"

void Player::draw(TextureManager *texManager) {
std::string fileName = "./res/" + name;
const char* image = fileName.c_str();
// TODO: Don't load this per draw, load it in advance
SDL_Texture *bitmapTex = texManager->LoadTexture(image);
int w, h;
SDL_QueryTexture(bitmapTex, NULL, NULL, &w, &h);
width = w;
height = h;
texManager->ClipTexture(bitmapTex, 0, 0, w, h, x, y, w, h);
}

void Player::sampleFromSpritesheet(TextureManager *texManager, SDL_Texture* bitmapTex, int pos) {
int sheet_w, sheet_h;
int num_characters_per_row, character_w, character_h;
character_w = 50;
character_h = 50;
character_w = width;
character_h = height;

SDL_QueryTexture(bitmapTex, NULL, NULL, &sheet_w, &sheet_h);
// std::cout << sheet_w << " " << sheet_h << " " << pos << std::endl;
Expand Down
2 changes: 0 additions & 2 deletions code/clonegame/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ class Player
int numSprites;
std::string name;
Player(int posx, int posy, std::string resName): x(posx), y(posy), name(resName) {};
void draw(TextureManager *texManager);
SDL_Texture* preloadImage(TextureManager *texManager, std::string name);
void sampleFromSpritesheet(TextureManager *texManager, SDL_Texture* bitmapTex, int pos);
};
Binary file added code/res/capguy-walk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 19b7f7a

Please sign in to comment.