Skip to content

Commit

Permalink
Fix random colours, no idea why it happened though
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 16, 2024
1 parent 6302e0c commit 95fb101
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 55 deletions.
41 changes: 7 additions & 34 deletions src/sdl/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ void MainMenu::init() {
return;
}

TextButton romButton(0, 25.0f * this->roms.size(), WIDTH, 25, text);

this->roms.emplace(file, romButton);
this->roms.emplace(file, TextButton(0, 25.0f * this->roms.size(), WIDTH, 25, text));
}
}

Expand All @@ -45,30 +43,6 @@ void MainMenu::init() {
return;
}
this->chooseFolder = std::make_unique<TextButton>(0, 400, WIDTH, 80, text);

// font = TTF_OpenFont("font.ttf", 32); // Ensure the path is correct
// if (!font) {
// // printf("TTF_OpenFont Error: %s\n", SDL_GetError());
// return;
// }
//
// // Create the text surface
// SDL_Color color = {0, 255, 0, 255}; // Green color
// text = TTF_RenderText_Solid(font, "Some test", 9, color);
// if (!text) {
// // printf("TTF_RenderText_Solid Error: %s\n", TTF_GetError());
// return;
// }
//
// // Create the text texture
// textTexture = SDL_CreateTextureFromSurface(renderer, text);
// if (!textTexture) {
// printf("SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError());
// SDL_DestroySurface(text); // Free the surface if texture creation fails
// return;
// }
//
// SDL_DestroySurface(text);
}

bool MainMenu::handleEvent(SDL_Event &event) {
Expand Down Expand Up @@ -105,15 +79,15 @@ void MainMenu::update() {
}

for (auto &romButton: roms) {
romButton.second.update(&this->inputHandler, point);
romButton.second.update(this->inputHandler, point);

if (!romButton.second.isJustClicked()) {
continue;
}

this->windows.emplace_back(std::make_unique<Emulator>(romButton.first))->init();
}
this->chooseFolder->update(&this->inputHandler, point);
this->chooseFolder->update(this->inputHandler, point);

if (this->chooseFolder->isJustClicked()) {

Expand Down Expand Up @@ -154,8 +128,7 @@ void MainMenu::update() {
return;
}

TextButton romButton(0, 25.0f * roms.size(), WIDTH, 25, text);
roms.emplace(file.path().string(), romButton);
roms.emplace(file.path().string(), TextButton(0, 25.0f * roms.size(), WIDTH, 25, text));
}
config.writeFile(configFilePath);
}
Expand All @@ -166,7 +139,7 @@ void MainMenu::update() {
}

void MainMenu::render() {
SDL_SetRenderDrawColor(this->renderer, 0x00, 0x00, 0x00, 0x00);
SDL_SetRenderDrawColor(this->renderer, 0x00, 0x00, 0x00, 255);
SDL_RenderClear(renderer);
for (auto &romButton: roms) {
romButton.second.draw(renderer);
Expand All @@ -185,11 +158,11 @@ void MainMenu::resize(SDL_Event& event) {

for (auto& romButton : roms) {
romButton.second.updateSize(this->originalSize, size);
romButton.second.update(&this->inputHandler, point);
romButton.second.update(this->inputHandler, point);
}

chooseFolder->updateSize(this->originalSize,size);
chooseFolder->update(&this->inputHandler, point);
chooseFolder->update(this->inputHandler, point);
}

void MainMenu::close() {
Expand Down
1 change: 1 addition & 0 deletions src/sdl/MainMenu.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef MAINMENU_H
#define MAINMENU_H

#include <string>
#include <unordered_map>
#include <vector>

Expand Down
32 changes: 16 additions & 16 deletions src/sdl/ui/TextButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

#include <iostream>

TextButton::TextButton(float x, float y, float width, float height, TTF_Text* text) : text(text) {
TextButton::TextButton(float x, float y, float width, float height, TTF_Text* text) :
text(text), idleColor(SDL_Color{192, 192, 192, 255}),
hoverColor(SDL_Color{128, 128, 128, 255}), activeColor(SDL_Color{64, 64, 64, 255}),
deleteColor(SDL_Color{255, 0, 0, 255}) {
this->button = SDL_FRect{x, y, width, height};

currentColor = idleColor;

TTF_SetTextColor(text, 0, 0, 0, 255);
SDL_Point textSize{};
TTF_GetTextSize(text, &textSize.x, &textSize.y);
Expand All @@ -14,15 +19,11 @@ TextButton::TextButton(float x, float y, float width, float height, TTF_Text* te
this->originalPosition = {button.x, button.y};
this->originalSize = {button.w, button.h};

this->idleColor = SDL_Color{192, 192, 192};
this->hoverColor = SDL_Color{128, 128, 128};
this->activeColor = SDL_Color{64, 64, 64};

this->isPressed = false;
this->isHovered = false;
}

void TextButton::updateSize(const SDL_Point originalSize, const SDL_Point updatedSize) {
void TextButton::updateSize(const SDL_Point& originalSize, const SDL_Point& updatedSize) {
this->button.x = this->originalPosition.x / originalSize.x * updatedSize.x;
this->button.y = this->originalPosition.y / originalSize.y * updatedSize.y;
this->button.w = this->originalSize.x / originalSize.x * updatedSize.x;
Expand All @@ -34,15 +35,15 @@ void TextButton::updateSize(const SDL_Point originalSize, const SDL_Point update
this->textPos.y = this->button.y + this->button.h / 2 - textSize.y / 2 + 4;
}

void TextButton::update(InputHandler* inputHandler, SDL_FPoint pos) {
void TextButton::update(InputHandler& inputHandler, SDL_FPoint& pos) {
this->lastPressed = this->isPressed;
this->isHovered = SDL_PointInRectFloat(&pos, &this->button);

if (this->isHovered && inputHandler->isJustClicked(SDL_BUTTON_LEFT)) {
if (this->isHovered && inputHandler.isJustClicked(SDL_BUTTON_LEFT)) {
this->isPressed = true;
updateColour(this->activeColor);
} else if (this->isHovered && inputHandler->isPressed(SDL_KMOD_LSHIFT)) {
updateColour( SDL_Color{255, 0, 0});
} else if (this->isHovered && inputHandler.isPressed(SDL_SCANCODE_LSHIFT)) {
updateColour(this->deleteColor);
} else if (this->isHovered) {
updateColour(this->hoverColor);
} else {
Expand All @@ -51,18 +52,17 @@ void TextButton::update(InputHandler* inputHandler, SDL_FPoint pos) {
}
}

void TextButton::updateColour(const SDL_Color color) {
if (this->color.r == color.r && this->color.g == color.g && this->color.b == color.b) {
return;
}
void TextButton::updateColour(SDL_Color& color) {
// if (this->currentColor == color) {
// return;
// }

this->currentColor = color;
}

void TextButton::draw(SDL_Renderer* window) const {
SDL_SetRenderDrawColor(window, this->currentColor.r, this->currentColor.g, this->currentColor.b, this->currentColor.a);
SDL_SetRenderDrawColor(window, currentColor.r, currentColor.g, currentColor.b, currentColor.a);
SDL_RenderFillRect(window, &this->button);
if (text == nullptr) return;
if (!TTF_DrawRendererText(text, textPos.x, textPos.y)) {
SDL_Log("Text rendering failed: %s\n", SDL_GetError());
}
Expand Down
9 changes: 4 additions & 5 deletions src/sdl/ui/TextButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define TEXTBUTTON_H

#include "../InputHandler.h"
#include <string>

#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
Expand All @@ -14,10 +13,10 @@ class TextButton {
SDL_FPoint originalPosition;
TTF_Text* text;
SDL_FPoint textPos;
SDL_Color color;
SDL_Color idleColor;
SDL_Color hoverColor;
SDL_Color activeColor;
SDL_Color deleteColor;
SDL_Color currentColor;
bool isPressed;
bool lastPressed{};
Expand All @@ -26,17 +25,17 @@ class TextButton {
public:
TextButton(float x, float y, float width, float height, TTF_Text* text);

void updateSize(SDL_Point originalSize, SDL_Point updatedSize);
void updateSize(const SDL_Point & originalSize, const SDL_Point & updatedSize);

void update(InputHandler* inputHandler, SDL_FPoint pos);
void update(InputHandler& inputHandler, SDL_FPoint& pos);

void draw(SDL_Renderer* window) const;

bool isClicked() const;

bool isJustClicked() const;

void updateColour(SDL_Color color);
void updateColour(SDL_Color& color);
};


Expand Down

0 comments on commit 95fb101

Please sign in to comment.