diff options
| m--------- | lib/SDL_ttf | 0 | ||||
| -rw-r--r-- | mwe/events/include/inputSystem.h | 8 | ||||
| -rw-r--r-- | mwe/events/include/uiObject.h | 19 | ||||
| -rw-r--r-- | mwe/events/include/uiRenderer.h | 4 | ||||
| -rw-r--r-- | mwe/events/src/inputSystem.cpp | 62 | ||||
| -rw-r--r-- | mwe/events/src/loopManager.cpp | 8 | ||||
| -rw-r--r-- | mwe/events/src/main.cpp | 18 | ||||
| -rw-r--r-- | mwe/events/src/uiObject.cpp | 2 | ||||
| -rw-r--r-- | mwe/events/src/uiRenderer.cpp | 75 | ||||
| -rw-r--r-- | mwe/events/src/window.cpp | 1 | ||||
| -rw-r--r-- | mwe/gameloop/include/loopManager.h | 4 | ||||
| -rw-r--r-- | mwe/gameloop/include/timer.h | 2 | ||||
| -rw-r--r-- | mwe/gameloop/src/loopManager.cpp | 21 | 
13 files changed, 164 insertions, 60 deletions
| diff --git a/lib/SDL_ttf b/lib/SDL_ttf new file mode 160000 +Subproject a3d0895c1b60c41ff9e85d9203ddd7485c014da diff --git a/mwe/events/include/inputSystem.h b/mwe/events/include/inputSystem.h index 06db74c..c20562d 100644 --- a/mwe/events/include/inputSystem.h +++ b/mwe/events/include/inputSystem.h @@ -8,9 +8,17 @@ class InputSystem {  public:      InputSystem();      void registerButton(Button* button); +	void registerText(Text* label); +	void registerTextInput(TextInput* input);      void processInput();  private:      std::vector<Button*> buttons; +	std::vector<TextInput*> textInputs; +	std::vector<Text*> texts;      void processMouseClick(int mouseX, int mouseY); +	void processInputField(int mouseX, int mouseY); +	void processKeyPress(Keycode); +	void processTextInput(const std::string& text); +	  }; diff --git a/mwe/events/include/uiObject.h b/mwe/events/include/uiObject.h index 720d5c2..893232d 100644 --- a/mwe/events/include/uiObject.h +++ b/mwe/events/include/uiObject.h @@ -53,17 +53,18 @@ class Text : public UIObject{  	TTF_Font *font;  	RGBColor color;  }; -class TextInput : public UIObject{ -	public: -	TextInput (int width,int height); -	std::string textBuffer; +class TextInput : public UIObject { +public: +    TextInput(int width, int height); +    std::string textBuffer;                std::string placeholder; -    size_t caretPosition; -    bool isActive; +    bool isActive = false;      RGBColor textColor;      RGBColor backgroundColor; -    size_t maxLength; +    size_t maxLength = 100;      Alignment alignment; -    TTF_Font* font; -	EventHandler<TextSubmitEvent> onSubmit; +    TTF_Font* font = nullptr; +    std::function<void()> onSubmit;  +	std::function<void()> onFocus;   }; + diff --git a/mwe/events/include/uiRenderer.h b/mwe/events/include/uiRenderer.h index 286ac1f..32acecf 100644 --- a/mwe/events/include/uiRenderer.h +++ b/mwe/events/include/uiRenderer.h @@ -1,15 +1,19 @@  #pragma once  #include <SDL2/SDL.h>  #include <SDL2/SDL_ttf.h> +#include <string>  #include "uiObject.h"  class UIRenderer {  public:      UIRenderer(SDL_Renderer* renderer); +    ~UIRenderer(); +      void render(UIObject* uiObject);  private:      SDL_Renderer* renderer; +    TTF_Font* font;      void renderButton(Button* button);      void renderText(Text* text); diff --git a/mwe/events/src/inputSystem.cpp b/mwe/events/src/inputSystem.cpp index ffab84b..bb26e8b 100644 --- a/mwe/events/src/inputSystem.cpp +++ b/mwe/events/src/inputSystem.cpp @@ -5,9 +5,14 @@ InputSystem::InputSystem() {}  void InputSystem::registerButton(Button* button) {      buttons.push_back(button);  } +void InputSystem::registerTextInput(TextInput* input) { +    textInputs.push_back(input); +} +void InputSystem::registerText(Text* label) { +    texts.push_back(label); +}  void InputSystem::processInput() { -	      SDL_Event event;      while (SDL_PollEvent(&event)) {          switch (event.type) { @@ -16,6 +21,11 @@ void InputSystem::processInput() {                  break;              case SDL_KEYDOWN:                  triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); +                processKeyPress(event.key.keysym.sym); +                break; +            case SDL_TEXTINPUT: +                // Process typed characters +                processTextInput(event.text.text);                  break;              case SDL_MOUSEBUTTONDOWN: {                  int mouseX, mouseY; @@ -28,6 +38,7 @@ void InputSystem::processInput() {      }  } +  void InputSystem::processMouseClick(int mouseX, int mouseY) {      for (auto* button : buttons) {          if (mouseX >= button->x && mouseX <= (button->x + button->width) && @@ -35,5 +46,54 @@ void InputSystem::processMouseClick(int mouseX, int mouseY) {              button->onClick();          }      } +	for(auto* textInput : textInputs){ +		if (mouseX >= textInput->x && mouseX <= textInput->x + textInput->width && +        mouseY >= textInput->y && mouseY <= textInput->y + textInput->height) { +        textInput->isActive = true; +    } else { +        textInput->isActive = false; +    } +	} +} +void InputSystem::processKeyPress(Keycode key) { +    // for (auto* textInput : textInputs) { +    //     if (textInput->isActive) { +    //         if (key == SDLK_RETURN || key == SDLK_KP_ENTER) { +    //             // Submit the text +    //             if (textInput->onSubmit) { +    //                 textInput->onSubmit(); +    //             } +    //         }  +    //         else if (key == SDLK_BACKSPACE) { +    //             // Handle backspace +    //             if (!textInput->textBuffer.empty() && textInput->caretPosition > 0) { +    //                 textInput->textBuffer.erase(textInput->caretPosition - 1, 1); +    //                 textInput->caretPosition--; +    //             } +    //         }  +    //         else if (key == SDLK_LEFT) { +    //             // Move caret left +    //             if (textInput->caretPosition > 0) { +    //                 textInput->caretPosition--; +    //             } +    //         }  +    //         else if (key == SDLK_RIGHT) { +    //             // Move caret right +    //             if (textInput->caretPosition < textInput->textBuffer.size()) { +    //                 textInput->caretPosition++; +    //             } +    //         } +    //     } +    // } +} + +void InputSystem::processTextInput(const std::string& text) { +    // for (auto* textInput : textInputs) { +    //     if (textInput->isActive) { +    //         // Insert text at caret position +    //         textInput->textBuffer.insert(textInput->caretPosition, text); +    //         textInput->caretPosition += text.length(); +    //     } +    // }  } diff --git a/mwe/events/src/loopManager.cpp b/mwe/events/src/loopManager.cpp index 83ccfc3..f673d44 100644 --- a/mwe/events/src/loopManager.cpp +++ b/mwe/events/src/loopManager.cpp @@ -80,6 +80,14 @@ void LoopManager::setup() {  	inputSystem->registerButton(testButton);  	window.addUIObject(testButton); + + +	TextInput* testInput = new TextInput(200,200); +	testInput->x = 100; +	testInput->y = 100; +	testInput->backgroundColor = {20,50,80}; +	inputSystem->registerTextInput(testInput); +	window.addUIObject(testInput);  }  void LoopManager::render() {  	//fprintf(stderr, "**********render********** \n"); diff --git a/mwe/events/src/main.cpp b/mwe/events/src/main.cpp index 03dff16..8fd6d10 100644 --- a/mwe/events/src/main.cpp +++ b/mwe/events/src/main.cpp @@ -40,7 +40,7 @@ void onKeyPressed(const KeyPressedEvent& e)  	fprintf(stderr,"second function KeyCode %d\n",keyCode);  }  void CollisionHandler(const CollisionEvent& e){ -	std::cout << "collision betwee object id: "<< e.getCollisionData().objectIdA << " and id: " << e.getCollisionData().objectIdB << std::endl; +	std::cout << "collision between object id: "<< e.getCollisionData().objectIdA << " and id: " << e.getCollisionData().objectIdB << std::endl;  }  void testCollisionEvent() {  	Collision testCollision(1, 2, {3, 4}, {5, 6}, 7.8f); @@ -69,19 +69,3 @@ int main(int argc, char * args[]) {  	gameLoop.loop();  	return 0;  } -// void collisionUpdate(){ -// 	int count; -// 	//iedere collision -// 	for (int i = 0; i < count; i++) -// 	{ -// 		//trigger object 1 -// 		//triger object 2 -// 		triggerEvent(CollisionEvent(1,2),1); -// 		triggerEvent(CollisionEvent(1,2),2); -// 	} -	 -// } -// int main(){ -	 -// 	return 0; -// } diff --git a/mwe/events/src/uiObject.cpp b/mwe/events/src/uiObject.cpp index ef4443f..31ff486 100644 --- a/mwe/events/src/uiObject.cpp +++ b/mwe/events/src/uiObject.cpp @@ -19,7 +19,7 @@ Text::Text(int width, int height)  }  TextInput::TextInput(int width, int height) -    : UIObject(width, height), textBuffer(""), placeholder(""), caretPosition(0),  +    : UIObject(width, height), textBuffer(""), placeholder(""),         isActive(false), textColor{255, 255, 255}, backgroundColor{0, 0, 0}, maxLength(100), font(nullptr) {      alignment.horizontal = Alignment::Horizontal::LEFT;      alignment.vertical = Alignment::Vertical::TOP; diff --git a/mwe/events/src/uiRenderer.cpp b/mwe/events/src/uiRenderer.cpp index 4a756f9..dbe8dfe 100644 --- a/mwe/events/src/uiRenderer.cpp +++ b/mwe/events/src/uiRenderer.cpp @@ -45,28 +45,59 @@ void UIRenderer::renderText(Text* text) {      }  } -// Private helper function to render a TextInput  void UIRenderer::renderTextInput(TextInput* textInput) { -    SDL_Rect inputRect = {textInput->x, textInput->y, textInput->width, textInput->height}; -    SDL_SetRenderDrawColor(renderer, textInput->backgroundColor.red, textInput->backgroundColor.green, textInput->backgroundColor.blue, 255); -    SDL_RenderFillRect(renderer, &inputRect); +    // // Check if textInput or renderer is null to avoid segmentation faults +    // if (!textInput || !renderer) { +    //     std::cerr << "Error: Null pointer detected for textInput or renderer." << std::endl; +    //     return; +    // } -    // Render text or placeholder -    if (!textInput->textBuffer.empty()) { -        SDL_Color sdlColor = {textInput->textColor.red, textInput->textColor.green, textInput->textColor.blue, 255}; -        SDL_Surface* textSurface = TTF_RenderText_Blended(textInput->font, textInput->textBuffer.c_str(), sdlColor); -        SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); -        SDL_Rect textRect = {textInput->x + 5, textInput->y + 5, textSurface->w, textSurface->h}; -        SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); -        SDL_FreeSurface(textSurface); -        SDL_DestroyTexture(textTexture); -    } else if (!textInput->placeholder.empty()) { -        SDL_Color sdlColor = {128, 128, 128, 255};  // Placeholder color -        SDL_Surface* placeholderSurface = TTF_RenderText_Blended(textInput->font, textInput->placeholder.c_str(), sdlColor); -        SDL_Texture* placeholderTexture = SDL_CreateTextureFromSurface(renderer, placeholderSurface); -        SDL_Rect placeholderRect = {textInput->x + 5, textInput->y + 5, placeholderSurface->w, placeholderSurface->h}; -        SDL_RenderCopy(renderer, placeholderTexture, nullptr, &placeholderRect); -        SDL_FreeSurface(placeholderSurface); -        SDL_DestroyTexture(placeholderTexture); -    } +    // // Render the background rectangle for the text input +    // SDL_Rect inputRect = {textInput->x, textInput->y, textInput->width, textInput->height}; +    // SDL_SetRenderDrawColor(renderer, textInput->backgroundColor.red, textInput->backgroundColor.green, textInput->backgroundColor.blue, 255); +    // SDL_RenderFillRect(renderer, &inputRect); + +    // // Check if font is valid +    // if (!textInput->font) { +    //     std::cerr << "Error: Font is not loaded for textInput." << std::endl; +    //     return; +    // } + +    // SDL_Color sdlColor = {textInput->textColor.red, textInput->textColor.green, textInput->textColor.blue, 255}; + +    // if (!textInput->textBuffer.empty()) { +    //     // Render the text in the input field +    //     SDL_Surface* textSurface = TTF_RenderText_Blended(textInput->font, textInput->textBuffer.c_str(), sdlColor); +    //     if (textSurface) { +    //         SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); +    //         if (textTexture) { +    //             SDL_Rect textRect = {textInput->x + 5, textInput->y + 5, textSurface->w, textSurface->h}; +    //             SDL_RenderCopy(renderer, textTexture, nullptr, &textRect); +    //             SDL_DestroyTexture(textTexture); +    //         } else { +    //             std::cerr << "Error: Unable to create texture from text surface." << std::endl; +    //         } +    //         SDL_FreeSurface(textSurface); +    //     } else { +    //         std::cerr << "Error: Unable to create text surface." << std::endl; +    //     } +    // } else if (!textInput->placeholder.empty()) { +    //     // Render the placeholder text +    //     SDL_Color placeholderColor = {128, 128, 128, 255}; // Light gray for placeholder +    //     SDL_Surface* placeholderSurface = TTF_RenderText_Blended(textInput->font, textInput->placeholder.c_str(), placeholderColor); +    //     if (placeholderSurface) { +    //         SDL_Texture* placeholderTexture = SDL_CreateTextureFromSurface(renderer, placeholderSurface); +    //         if (placeholderTexture) { +    //             SDL_Rect placeholderRect = {textInput->x + 5, textInput->y + 5, placeholderSurface->w, placeholderSurface->h}; +    //             SDL_RenderCopy(renderer, placeholderTexture, nullptr, &placeholderRect); +    //             SDL_DestroyTexture(placeholderTexture); +    //         } else { +    //             std::cerr << "Error: Unable to create texture from placeholder surface." << std::endl; +    //         } +    //         SDL_FreeSurface(placeholderSurface); +    //     } else { +    //         std::cerr << "Error: Unable to create placeholder surface." << std::endl; +    //     } +    // }  } + diff --git a/mwe/events/src/window.cpp b/mwe/events/src/window.cpp index a3bae40..7192e6b 100644 --- a/mwe/events/src/window.cpp +++ b/mwe/events/src/window.cpp @@ -36,7 +36,6 @@ bool WindowManager::initWindow() {  }  void WindowManager::destroyWindow() { -    delete uiRenderer;      SDL_DestroyRenderer(renderer);      SDL_DestroyWindow(window);      SDL_Quit(); diff --git a/mwe/gameloop/include/loopManager.h b/mwe/gameloop/include/loopManager.h index 1befb69..e2c2e2c 100644 --- a/mwe/gameloop/include/loopManager.h +++ b/mwe/gameloop/include/loopManager.h @@ -1,11 +1,11 @@  #pragma once -#include "event.h"  #include "gameObject.h"  #include "window.h"  #include <SDL2/SDL.h>  class LoopManager {  public:  	LoopManager(); +	~LoopManager();  	void setup();  	void loop(); @@ -17,7 +17,7 @@ private:  	void fixedUpdate();  	void render();  	bool gameRunning = false; -	WindowManager window; +	WindowManager* window;  	int timeScale = 1;  	float accumulator = 0.0;  	double currentTime; diff --git a/mwe/gameloop/include/timer.h b/mwe/gameloop/include/timer.h index 8273746..3c38594 100644 --- a/mwe/gameloop/include/timer.h +++ b/mwe/gameloop/include/timer.h @@ -25,7 +25,7 @@ private:  	double maximumDeltaTime = 0.25;  	double deltaTime;  	double frameTargetTime = FPS / 1000; -	double fixedDeltaTime = 0.01; +	double fixedDeltaTime = 0.02;  	double elapsedTime;  	double elapsedFixedTime;  	double time; diff --git a/mwe/gameloop/src/loopManager.cpp b/mwe/gameloop/src/loopManager.cpp index 0392853..b075bd8 100644 --- a/mwe/gameloop/src/loopManager.cpp +++ b/mwe/gameloop/src/loopManager.cpp @@ -1,6 +1,15 @@  #include "loopManager.h"  #include "timer.h" -LoopManager::LoopManager() {} +LoopManager::LoopManager() { +	this->window = new WindowManager(); +	 +} +LoopManager::~LoopManager(){ +	for(GameObject* object : this->objectList){ +		delete object; +	} +	delete this->window; +}  void LoopManager::processInput() {  	SDL_Event event;  	SDL_PollEvent(&event); @@ -42,14 +51,14 @@ void LoopManager::loop() {  		timer.enforceFrameRate();  	} -	window.destroyWindow(); +	window->destroyWindow();  }  void LoopManager::setup() { -	gameRunning = window.initWindow(); -	LoopTimer::getInstance().start(); -	LoopTimer::getInstance().setFPS(500); +	LoopTimer::getInstance().start(); +	LoopTimer::getInstance().setFPS(10); +	this->gameRunning = true;  	for (int i = 1; i < 3; i++) {  		GameObject * square  			= new GameObject("square2", i * 60, i * 60, 20, 20, 0, 0); @@ -59,7 +68,7 @@ void LoopManager::setup() {  void LoopManager::render() {  	fprintf(stderr, "**********render********** \n");  	if (gameRunning) { -		window.render(objectList); +		window->render(objectList);  	}  } |