Skip to content

Commit

Permalink
feat: setup project with cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
AasmundN committed Dec 18, 2023
1 parent 5bf2949 commit ed2c540
Show file tree
Hide file tree
Showing 16 changed files with 334 additions and 363 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
bin
bin
build
21 changes: 0 additions & 21 deletions .vscode/c_cpp_properties.json

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/extensions.json

This file was deleted.

20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/build/flappy-bird",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
21 changes: 1 addition & 20 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
{
"actionButtons": {
"reloadButton": null,
"defaultColor": "#ff0034", // Can also use string color names.
"loadNpmCommands": false, // Disables automatic generation of actions for npm commands.
"commands": [
{
"name": "Run",
"color": "green",
"singleInstance": true,
"command": "./bin/main" // This is executed in the terminal.
},
{
"name": "Build",
"color": "green",
"singleInstance": true,
"command": "./comprun.sh" // This is executed in the terminal.
}
]
},
// cpp formatting
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 3, TabWidth: 3, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }"
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }"
}
37 changes: 37 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.5.0)
project(flappy-bird VERSION 0.1.0 LANGUAGES C CXX)

include(CTest)
enable_testing()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
set(SOURCES
src/main.cpp
src/bird.cpp
src/game.cpp
src/pipe.cpp
)

add_executable(${PROJECT_NAME} ${SOURCES})

target_link_libraries(
${PROJECT_NAME} PRIVATE
/Library/Frameworks/SDL2.framework/Versions/A/SDL2
/Library/Frameworks/SDL2_image.framework/Versions/A/SDL2_image
/Library/Frameworks/SDL2_ttf.framework/Versions/A/SDL2_ttf
)

target_include_directories(
${PROJECT_NAME} PRIVATE
/Library/Frameworks/SDL2.framework/Versions/A/Headers
/Library/Frameworks/SDL2_image.framework/Versions/A/Headers
/Library/Frameworks/SDL2_ttf.framework/Versions/A/Headers
)

target_include_directories(${PROJECT_NAME} PRIVATE include)

file(COPY graphics DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
18 changes: 0 additions & 18 deletions comprun.sh

This file was deleted.

24 changes: 12 additions & 12 deletions include/bird.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
#include "entity.h"

class Bird : public Entity {
public:
Bird(SDL_Renderer &renderer, SDL_Texture &texture, int width, int height, int x, int y);
virtual ~Bird() = default;
public:
Bird(SDL_Renderer &renderer, SDL_Texture &texture, int width, int height, int x, int y);
virtual ~Bird() = default;

void render();
void update();
void jump();
void render();
void update();
void jump();

int getY() { return rect.y; }
int getY() { return rect.y; }

private:
int velocity, acceleration, jumpState, animationState;
double angle;
void updateJumpState();
SDL_Rect srcrect;
private:
int velocity, acceleration, jumpState, animationState;
double angle;
void updateJumpState();
SDL_Rect srcrect;
};

#endif
18 changes: 9 additions & 9 deletions include/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#include <SDL.h>

class Entity {
public:
Entity(SDL_Renderer &renderer, SDL_Texture &texture) : renderer(renderer), texture(texture), rect({0, 0, 0, 0}) {}
virtual ~Entity() = default;
public:
Entity(SDL_Renderer &renderer, SDL_Texture &texture) : renderer(renderer), texture(texture), rect({0, 0, 0, 0}) {}
virtual ~Entity() = default;

virtual void render() = 0;
virtual void update() = 0;
virtual void render() = 0;
virtual void update() = 0;

protected:
SDL_Rect rect;
SDL_Renderer &renderer;
SDL_Texture &texture;
protected:
SDL_Rect rect;
SDL_Renderer &renderer;
SDL_Texture &texture;
};

#endif
56 changes: 28 additions & 28 deletions include/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,44 @@ struct _TTF_Font;

namespace flappy_bird {
class Game final {
public:
Game(int width, int height);
~Game();
public:
Game(int width, int height);
~Game();

int run();
int run();

private:
enum class State {
RUNNING,
STOPPED,
LOST
};
private:
enum class State {
RUNNING,
STOPPED,
LOST
};

int width, height;
int width, height;

SDL_Window *window;
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Renderer *renderer;

std::vector<SDL_Texture *> pipeTextures;
SDL_Texture *birdTexture;
_TTF_Font *font;
std::vector<SDL_Texture *> pipeTextures;
SDL_Texture *birdTexture;
_TTF_Font *font;

State state;
State state;

SDL_Rect scoreDst;
int score;
SDL_Rect scoreDst;
int score;

std::shared_ptr<Bird> bird;
std::vector<std::shared_ptr<Pipe>> pipes;
std::shared_ptr<Bird> bird;
std::vector<std::shared_ptr<Pipe>> pipes;

unsigned long frameStart;
unsigned long frameTime;
static const int FPS = 40;
unsigned long frameStart;
unsigned long frameTime;
static const int FPS = 40;

void updateFrame();
void drawFrame();
void collide();
void drawScore();
void updateFrame();
void drawFrame();
void collide();
void drawScore();
};
} // namespace flappy_bird

Expand Down
26 changes: 13 additions & 13 deletions include/pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
#include "entity.h"

class Pipe : public Entity {
public:
Pipe(SDL_Renderer &renderer, SDL_Texture &texture, int width, int height, int interval, int speed);
virtual ~Pipe() = default;
void render();
void update();
public:
Pipe(SDL_Renderer &renderer, SDL_Texture &texture, int width, int height, int interval, int speed);
virtual ~Pipe() = default;
void render();
void update();

int getX() { return rect.x; }
int getShift() { return shift; }
int getX() { return rect.x; }
int getShift() { return shift; }

bool isPassed() { return passed; }
void setPassed() { passed = true; }
bool isPassed() { return passed; }
void setPassed() { passed = true; }

private:
int interval, width, height, shift;
int speed;
bool passed;
private:
int interval, width, height, shift;
int speed;
bool passed;
};

#endif
26 changes: 0 additions & 26 deletions makefile

This file was deleted.

Loading

0 comments on commit ed2c540

Please sign in to comment.