Skip to content

Commit

Permalink
Merge pull request #45 from NewayPix/add-clangd-and-review-project
Browse files Browse the repository at this point in the history
Add clangd config, format and review project issues
  • Loading branch information
ryukinix authored Dec 25, 2024
2 parents f55e14f + 37f44aa commit ceac601
Show file tree
Hide file tree
Showing 21 changed files with 832 additions and 431 deletions.
400 changes: 400 additions & 0 deletions .clang-format

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CompileFlags:
Add: ["-Wall", "-Wextra", "-std=c++17"]
Diagnostics:
MissingIncludes: Strict
UnusedIncludes: Strict
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
CXX = g++
SRC_DIR = src
INCLUDES = $(shell pkg-config --cflags sdl2) -I $(SRC_DIR)
INCLUDES = $(shell pkg-config --cflags sdl2) -I $(SRC_DIR)
CXXFLAGS = -w $(INCLUDES) -g -Wall -Wextra -Werror -DDEBUG -std=c++17
LFLAGS = -lSDL2 -lm -lSDL2_gfx
MAIN = $(SRC_DIR)/main.cpp
SRCS = $(shell find $(SRC_DIR) -name "*.cpp" -type f)
HEADERS = $(shell find $(SRC_DIR) -name "*.hpp" -type f)
LIB_SRCS = $(filter-out $(MAIN), $(SRCS))
TESTS = $(wildcard tests/*.cpp)
EXAMPLES = $(shell find examples -name "*.cpp" -type f)
Expand All @@ -30,6 +31,12 @@ examples/%.cpp: $(LIB_OBJS) FORCE
%.bin: FORCE
./$@

format/%:
clang-format -i $*

format: $(SRCS)
clang-format -i $(SRCS) $(HEADERS) $(EXAMPLES)

examples: $(EXAMPLES)

tests: $(TESTS_OBJS)
Expand Down
1 change: 1 addition & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--include-directory=src/
136 changes: 67 additions & 69 deletions examples/basics/square-moving.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include <iostream>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <cmath>
#include <string>
#include <iostream>
#include <map>

#include <SDL2/SDL.h>

#include "math/Vector2.hpp"
#include "InputHandler.hpp"
#include "Game.hpp"
#include "InputHandler.hpp"
#include "math/Vector2.hpp"

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
Expand All @@ -16,13 +19,9 @@ using namespace std;

// keys to InputHandler observe
std::map<const char*, SDL_Keycode> input_config = {
{"quit", SDLK_ESCAPE},
{"up", SDLK_UP},
{"left", SDLK_LEFT},
{"right", SDLK_RIGHT},
{"down", SDLK_DOWN},
{"velocity_up", SDLK_w},
{"velocity_down", SDLK_s},
{ "quit", SDLK_ESCAPE }, { "up", SDLK_UP }, { "left", SDLK_LEFT },
{ "right", SDLK_RIGHT }, { "down", SDLK_DOWN }, { "velocity_up", SDLK_w },
{ "velocity_down", SDLK_s },
};

struct KeyboardState {
Expand All @@ -39,15 +38,15 @@ struct KeyboardState {
};

struct Player {
Vector2 position = {0, 0};
Vector2 direction = {0, 0};
int size = 50;
SDL_Rect rect = {0, 0, size, size};
float velocity = 300;

void set_direction(struct KeyboardState *k) {
direction.x = k->right? 1: k->left? -1: 0;
direction.y = k->up? 1: k->down? -1: 0;
Vector2 position = { 0, 0 };
Vector2 direction = { 0, 0 };
int size = 50;
SDL_Rect rect = { 0, 0, size, size };
float velocity = 300;

void set_direction(struct KeyboardState* k) {
direction.x = k->right ? 1 : k->left ? -1 : 0;
direction.y = k->up ? 1 : k->down ? -1 : 0;
}

float cos_direction() {
Expand All @@ -57,7 +56,7 @@ struct Player {

float sin_direction() {
float cos = cos_direction();
float sin = sqrt(1 - cos*cos);
float sin = sqrt(1 - cos * cos);
return -direction.y * sin;
}

Expand All @@ -67,74 +66,73 @@ struct Player {
}
};


struct Player player = {};
struct Player player = {};
struct KeyboardState keyboard = {};
InputHandler input_handler = InputHandler(input_config);
InputHandler input_handler = InputHandler(input_config);

void Game::event() {
static SDL_Event e;
input_handler.process(e);
keyboard.up = input_handler.read("up");
keyboard.down = input_handler.read("down");
keyboard.left = input_handler.read("left");
keyboard.right = input_handler.read("right");
keyboard.velocity_up = input_handler.read("velocity_up");
keyboard.up = input_handler.read("up");
keyboard.down = input_handler.read("down");
keyboard.left = input_handler.read("left");
keyboard.right = input_handler.read("right");
keyboard.velocity_up = input_handler.read("velocity_up");
keyboard.velocity_down = input_handler.read("velocity_down");
this->running = !(input_handler.read(SDL_QUIT) || input_handler.read("quit"));
this->running = !(input_handler.read(SDL_QUIT) || input_handler.read("quit"));
}

void Game::update(float dt) {
if (keyboard.velocity_up) {
player.velocity += 50;
input_handler.write("velocity_up", false);
}
if (keyboard.velocity_down) {
player.velocity -= 50;
if (player.velocity <= 50) player.velocity = 50;
input_handler.write("velocity_down", false);
}

if (keyboard.valid_move()) {
player.set_direction(&keyboard);
player.move(dt);
}

if (player.position.x < 0) {
player.position.x = 0;
} else if (player.position.x + player.size > SCREEN_WIDTH) {
player.position.x = SCREEN_WIDTH - player.size;
}
if (player.position.y < 0) {
player.position.y = 0;
} else if (player.position.y + player.size > SCREEN_HEIGHT) {
player.position.y = SCREEN_HEIGHT - player.size;
}

player.rect.x = round(player.position.x);
player.rect.y = round(player.position.y);
if (keyboard.velocity_up) {
player.velocity += 50;
input_handler.write("velocity_up", false);
}
if (keyboard.velocity_down) {
player.velocity -= 50;
if (player.velocity <= 50)
player.velocity = 50;
input_handler.write("velocity_down", false);
}

if (keyboard.valid_move()) {
player.set_direction(&keyboard);
player.move(dt);
}

if (player.position.x < 0) {
player.position.x = 0;
} else if (player.position.x + player.size > SCREEN_WIDTH) {
player.position.x = SCREEN_WIDTH - player.size;
}
if (player.position.y < 0) {
player.position.y = 0;
} else if (player.position.y + player.size > SCREEN_HEIGHT) {
player.position.y = SCREEN_HEIGHT - player.size;
}

player.rect.x = round(player.position.x);
player.rect.y = round(player.position.y);
}

void Game::draw() {
// background
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
SDL_RenderClear(this->renderer);
// background
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
SDL_RenderClear(this->renderer);

// player
SDL_SetRenderDrawColor(this->renderer, 0, 255, 255, 255);
SDL_RenderFillRect(this->renderer, &player.rect);
// player
SDL_SetRenderDrawColor(this->renderer, 0, 255, 255, 255);
SDL_RenderFillRect(this->renderer, &player.rect);

// render everything
SDL_RenderPresent(this->renderer);
// render everything
SDL_RenderPresent(this->renderer);
}

void start() {
cout << ":: Game initialization!" << endl;
player.position.x = SCREEN_WIDTH / 2;
player.position.y = SCREEN_HEIGHT / 2;
player.position.x = static_cast<float>(SCREEN_WIDTH) / 2;
player.position.y = static_cast<float>(SCREEN_HEIGHT) / 2;
}


int main(void) {
start();
Game game("Square Moving", SCREEN_WIDTH, SCREEN_HEIGHT);
Expand Down
Loading

0 comments on commit ceac601

Please sign in to comment.