Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# IDE Files
.vscode/

# emscripten-generated files
index.data
index.html
index.js
index.wasm

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ winObjCompile:
mkdir -p ./make/build
${CC} ./src/*.cpp ${CFLAGSO} -mwindows
# laymans way to move object files to make/build folder
mv *.o ./make/build
mv *.o ./make/build

web:
em++ -std=c++14 -Wall src/main.cpp src/entity.cpp src/renderwindow.cpp src/player.cpp src/ground.cpp src/groundtile.cpp -I include -O2 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS=\['png'\] -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2 --preload-file res -o index.html

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ After installing the dev packages of SDL2 for your distribution, execute the fol
g++ -c src/*.cpp -std=c++14 -O3 -Wall -m64 -I include && mkdir -p bin/release && g++ *.o -o bin/release/main -s -lSDL2main -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
```
The compiled binary ``main`` is located in ``./bin``. For it to run, you must copy the ``./res`` folder to its directory.

### Web
Install [emscripten](https://emscripten.org/docs/getting_started/downloads.html) and execute the following command in the project's root directory:
```
emcc src/main.cpp src/entity.cpp src/renderwindow.cpp src/player.cpp src/ground.cpp src/groundtile.cpp -I include -O2 -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s \"SDL2_IMAGE_FORMATS=['png']\" -s USE_SDL_TTF=2 -s USE_SDL_MIXER=2 --preload-file res -o index.html
```

You can also just run `make web` to achieve the same compilation.

The compiled ``.js``, ``.wasm``, ``.data``, and ``.html`` files are located in the project's root.

You can use `python` to run a server on localhost port 8000 like so:

```
python -m SimpleHTTPServer
```

and visit [http://localhost:8000](http://localhost:8000) to run the game in the browser.


## Contributing
Pull requests are welcome! For major refactors, please open an issue first to discuss what you would like to improve. Feel free to create a fork of this repository or use the code for any other noncommercial purposes.
4 changes: 2 additions & 2 deletions include/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Player : public Entity {
float distanceFromCursor();
bool jump();
void update(Ground& ground);
const char* getScore();
const char* getHighscore();
std::string getScore();
std::string getHighscore();
int getScoreInt();
int isDead();
void reset();
Expand Down
1 change: 0 additions & 1 deletion src/groundtile.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "groundtile.h"

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 480;

GroundTile::GroundTile(SDL_Texture* p_tex, int p_index)
Expand Down
10 changes: 7 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,14 @@ void gameLoop()
window.render(ground.getTile(i));
}
window.render(25, 30, arrow);
window.render(62, 20, player.getScore(), font32_outline, black);
window.render(65, 23, player.getScore(), font32, white);

std::string distanceScore = player.getScore();
std::string highScore = player.getHighscore();

window.render(62, 20, distanceScore.c_str(), font32_outline, black);
window.render(65, 23, distanceScore.c_str(), font32, white);
window.render(0, 65, highscoreBox);
window.render(65, 64, player.getHighscore(), font16, white);
window.render(65, 64, highScore.c_str(), font16, white);

if (player.isDead() != ALIVE)
{
Expand Down
10 changes: 5 additions & 5 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>
#include <vector>
#include <string>
#include <iostream>

#include "player.h"
#include "entity.h"
Expand All @@ -11,7 +12,6 @@
const float GRAVITY = 0.09f;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 480;
const int ALIVE = 0;
const int CURSOR_DEATH = 1;
const int HOLE_DEATH = 2;

Expand Down Expand Up @@ -138,18 +138,18 @@ void Player::update(Ground& ground)

}

const char* Player::getScore()
std::string Player::getScore()
{
std::string s = std::to_string(score);
s = "DISTANCE: " + s;
return s.c_str();
return s;
}

const char* Player::getHighscore()
std::string Player::getHighscore()
{
std::string s = std::to_string(highscore);
s = "BEST: " + s;
return s.c_str();
return s;
}

int Player::getScoreInt()
Expand Down
2 changes: 1 addition & 1 deletion src/renderwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void RenderWindow::render(SDL_Texture* p_tex)

void RenderWindow::render(float p_x, float p_y, const char* p_text, TTF_Font* font, SDL_Color textColor)
{
SDL_Surface* surfaceMessage = TTF_RenderText_Blended( font, p_text, textColor);
SDL_Surface* surfaceMessage = TTF_RenderText_Blended(font, p_text, textColor);
SDL_Texture* message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);

SDL_Rect src;
Expand Down