Skip to content

Commit

Permalink
feat: highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
developer239 committed Dec 3, 2023
1 parent f4893b8 commit cb04670
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 114 deletions.
9 changes: 7 additions & 2 deletions src/apps/day3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ set(SOURCES
src/events/KeyPressedEvent.h
src/systems/CollisionSystem.h
src/systems/KeyboardControlSystem.h
src/systems/MovementSystem.h
src/systems/RenderRigidBodiesSystem.h
src/strategies/ECSStrategy.h src/services/PuzzleInputParser.h src/components/TextComponent.h src/systems/RenderTextSystem.h src/systems/RenderGridSystem.h src/systems/RenderColliderSystem.h src/systems/PuzzleSolverSystem.h)
src/strategies/ECSStrategy.h
src/services/PuzzleInputParser.h
src/components/TextComponent.h
src/systems/RenderTextSystem.h
src/systems/RenderGridSystem.h
src/systems/RenderColliderSystem.h
src/systems/PuzzleSolverSystem.h)

add_executable(${APP_NAME} ${SOURCES})

Expand Down
7 changes: 4 additions & 3 deletions src/apps/day3/src/components/RigidBodyComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ struct RigidBodyComponent {
int height;
Vec2 velocity;
Vec2 position;
bool filled; // Indicates whether the body is filled or just a border
bool filled;
SDL_Color color;

RigidBodyComponent(
int width = 0, int height = 0, Vec2 position = Vec2(0.0, 0.0),
Vec2 velocity = Vec2(0.0, 0.0), bool filled = true
Vec2 velocity = Vec2(0.0, 0.0), bool filled = true, SDL_Color color = {255, 255, 255, 255}
)
: width(width), height(height), velocity(velocity), position(position), filled(filled) {}
: width(width), height(height), velocity(velocity), position(position), filled(filled), color(color) {}
};
1 change: 0 additions & 1 deletion src/apps/day3/src/strategies/ECSStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "../services/PuzzleInputParser.h"
#include "../systems/CollisionSystem.h"
#include "../systems/KeyboardControlSystem.h"
#include "../systems/MovementSystem.h"
#include "../systems/PuzzleSolverSystem.h"
#include "../systems/RenderColliderSystem.h"
#include "../systems/RenderGridSystem.h"
Expand Down
88 changes: 0 additions & 88 deletions src/apps/day3/src/systems/MovementSystem.h

This file was deleted.

6 changes: 5 additions & 1 deletion src/apps/day3/src/systems/PuzzleSolverSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ class PuzzleSolverSystem : public ECS::System {

// Ensure one is a gear part and the other is a symbol
if ((isAGearPart && isBSymbol) || (isBGearPart && isASymbol)) {
ECS::Entity gearPartEntity = isAGearPart ? event.a : event.b;
partEntities.insert(
isAGearPart ? event.a : event.b
gearPartEntity
); // Insert the gear part

auto& rigidBody = registry.GetComponent<RigidBodyComponent>(gearPartEntity);
rigidBody.color = {50, 255, 50, 255};
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/apps/day3/src/systems/RenderColliderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ class RenderCollidersSystem : public ECS::System {
SDL_RenderDrawRect(renderer.Get().get(), &colliderRect);
}

// Reset the renderer color to avoid affecting other render calls
SDL_SetRenderDrawColor(
renderer.Get().get(),
0,
0,
0,
255
); // Back to black or another default color
);
}
};
17 changes: 6 additions & 11 deletions src/apps/day3/src/systems/RenderGridSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,52 @@ class RenderGridSystem : public ECS::System {
: scaleX(scaleX), scaleY(scaleY) {}

void Render(Core::Renderer renderer, Core::Window window) {
// Set the color for the grid lines
SDL_SetRenderDrawColor(
renderer.Get().get(),
200,
200,
200,
SDL_ALPHA_OPAQUE
); // Light grey color
);

// Calculate the number of lines to draw based on the window size and scale
int numHorizontalLines = window.GetHeight() / scaleY;
int numVerticalLines = window.GetWidth() / scaleX;

// Draw horizontal lines
for (int i = 0; i <= numHorizontalLines; ++i) {
int y = i * scaleY;
for (int x = 0; x < window.GetWidth();
x += 10) { // Draw dotted line: 10 pixels line, 10 pixels space
x += 10) {
SDL_RenderDrawLine(
renderer.Get().get(),
x,
y,
x + 5,
y
); // Draw 5 pixels
);
}
}

// Draw vertical lines
for (int i = 0; i <= numVerticalLines; ++i) {
int x = i * scaleX;
for (int y = 0; y < window.GetHeight();
y += 10) { // Draw dotted line: 10 pixels line, 10 pixels space
y += 10) {
SDL_RenderDrawLine(
renderer.Get().get(),
x,
y,
x,
y + 5
); // Draw 5 pixels
);
}
}

// Reset the color for subsequent renderings
SDL_SetRenderDrawColor(
renderer.Get().get(),
0,
0,
0,
SDL_ALPHA_OPAQUE
); // Back to black
);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/apps/day3/src/systems/RenderRigidBodiesSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RenderRigidBodiesSystem : public ECS::System {
(int)rigidBodyComponent.position.y,
(int)rigidBodyComponent.width,
(int)rigidBodyComponent.height};
SDL_SetRenderDrawColor(renderer.Get().get(), 255, 255, 255, 255);
SDL_SetRenderDrawColor(renderer.Get().get(), rigidBodyComponent.color.r, rigidBodyComponent.color.g, rigidBodyComponent.color.b, rigidBodyComponent.color.a);
SDL_RenderFillRect(renderer.Get().get(), &rect);
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/apps/day3/src/systems/RenderTextSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,27 @@ class RenderTextSystem : public ECS::System {
const auto& textLabel = ECS::Registry::Instance().GetComponent<TextComponent>(entity);
const auto& rigidBody = ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);

// Use the font from the asset store
TTF_Font* font = Core::AssetStore::Instance().GetFont(textLabel.fontId).get();
if (!font) {
// Handle error: Font not found
continue;
}

SDL_Surface* surface = TTF_RenderText_Blended(font, textLabel.text.c_str(), textLabel.color);
if (!surface) {
// Handle error: Unable to create surface
continue;
}

SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer.Get().get(), surface);
SDL_FreeSurface(surface);
if (!texture) {
// Handle error: Unable to create texture
continue;
}

int textWidth = 0, textHeight = 0;
SDL_QueryTexture(texture, nullptr, nullptr, &textWidth, &textHeight);

// Center the text within the rigid body
// Center

int xPosition = static_cast<int>(rigidBody.position.x + (rigidBody.width - textWidth) / 2);
int yPosition = static_cast<int>(rigidBody.position.y + (rigidBody.height - textHeight) / 2);

Expand Down

0 comments on commit cb04670

Please sign in to comment.