Write 2D text game engine on C++, using SDL3 and any other libraries on your wish.
- User
Write 2D text game engine on C++, using SDL3 and any other libraries on your wish.
- Assistant
Write 2D text game engine on C++, using SDL3 and any other libraries on your wish.
Certainly! Below is a basic 2D text game engine written in C++ using SDL3 and SDL_ttf. This engine will initialize SDL, create a window and renderer, handle keyboard input, render text, and manage a simple game loop. You can expand upon this foundation to create text-based games with interactive elements.
Prerequisites:
Install SDL3 and SDL_ttf:
sudo apt-get install libsdl3-dev libsdl3-ttf-devInclude Directories and Libraries:
Font File:
Arial.ttf) and place it in your project directory or specify the path in the code.// main.cpp
#include <SDL3/SDL.h>
#include <SDL3/SDL_ttf.h>
#include <iostream>
#include <string>
// Screen dimensions
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
// Global variables
SDL_Window* gWindow = nullptr;
SDL_Renderer* gRenderer = nullptr;
TTF_Font* gFont = nullptr;
// Function to initialize SDL and SDL_ttf
bool init() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << '\n';
return false;
}
// Create window
gWindow = SDL_CreateWindow("2D Text Game Engine",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (!gWindow) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << '\n';
return false;
}
// Create renderer
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (!gRenderer) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << '\n';
return false;
}
// Initialize SDL_ttf
if (TTF_Init() == -1) {
std::cerr << "SDL_ttf could not initialize! SDL_ttf Error: " << TTF_GetError() << '\n';
return false;
}
// Load font
gFont = TTF_OpenFont("Arial.ttf", 24); // Replace with your font file
if (!gFont) {
std::cerr << "Failed to load font! SDL_ttf Error: " << TTF_GetError() << '\n';
return false;
}
return true;
}
// Function to load text into an SDL_Texture
SDL_Texture* loadTextTexture(const std::string& text, SDL_Color color) {
// Render text surface
SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, text.c_str(), color);
if (!textSurface) {
std::cerr << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << '\n';
return nullptr;
}
// Create texture from surface pixels
SDL_Texture* texture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
if (!texture) {
std::cerr << "Unable to create texture from rendered text! SDL_Error: " << SDL_GetError() << '\n';
}
// Clean up
SDL_FreeSurface(textSurface);
return texture;
}
// Function to free resources and close SDL
void close() {
// Free font
TTF_CloseFont(gFont);
gFont = nullptr;
// Destroy renderer and window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gRenderer = nullptr;
gWindow = nullptr;
// Quit SDL subsystems
TTF_Quit();
SDL_Quit();
}
int main(int argc, char* argv[]) {
if (!init()) {
std::cerr << "Failed to initialize!\n";
return -1;
}
bool quit = false;
SDL_Event e;
std::string inputText = "Enter text: ";
SDL_Color textColor = { 255, 255, 255, 255 }; // White color
// Enable text input
SDL_StartTextInput();
while (!quit) {
// Event handling
while (SDL_PollEvent(&e)) {
// User requests quit
if (e.type == SDL_EVENT_QUIT) {
quit = true;
}
// Key pressed
else if (e.type == SDL_EVENT_KEY_DOWN) {
// Handle backspace
if (e.key.keysym.sym == SDLK_BACKSPACE && inputText.length() > 0) {
// Erase character
inputText.pop_back();
}
// Handle return (enter key)
else if (e.key.keysym.sym == SDLK_RETURN) {
// Process inputText or reset
inputText = "Enter text: ";
}
}
// Text input
else if (e.type == SDL_EVENT_TEXT_INPUT) {
// Append character
inputText += e.text.text;
}
}
// Clear screen
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255); // Black background
SDL_RenderClear(gRenderer);
// Render current text
SDL_Texture* textTexture = loadTextTexture(inputText, textColor);
if (textTexture) {
// Get texture dimensions
int textWidth, textHeight;
SDL_QueryTexture(textTexture, nullptr, nullptr, &textWidth, &textHeight);
// Define destination rectangle
SDL_FRect renderQuad = { 50.0f, 50.0f, static_cast<float>(textWidth), static_cast<float>(textHeight) };
// Render to screen
SDL_RenderCopy(gRenderer, textTexture, nullptr, &renderQuad);
// Clean up
SDL_DestroyTexture(textTexture);
}
// Update screen
SDL_RenderPresent(gRenderer);
}
// Disable text input and close
SDL_StopTextInput();
close();
return 0;
}
Initialization:
Event Loop:
inputText string with the user's input.Rendering:
inputText onto a texture.SDL_RenderPresent.Cleanup:
Ensure you have the SDL3 and SDL_ttf development libraries installed and properly linked.
Using g++ Compiler:
g++ -std=c++17 -o text_game_engine main.cpp $(sdl2-config --cflags --libs) -lSDL3_ttf
Note: Replace sdl2-config with the appropriate command for SDL3 if available. If SDL3 provides sdl3-config, use that instead.
Include Paths and Libraries Manually:
If sdl3-config is not available, you might have to specify include directories and library paths manually:
g++ -std=c++17 -I/path/to/SDL3/include -L/path/to/SDL3/lib -o text_game_engine main.cpp -lSDL3 -lSDL3_ttf
Font File:
"Arial.ttf" in the code with the path to your font file.SDL3 Adjustments:
Event Handling:
Game Expansion:
std::cerr when failures occur.This minimalist 2D text game engine serves as a foundation for building text-based games in C++. By leveraging SDL3 and SDL_ttf, you can handle user input, render text, and manage game states efficiently. Expand upon this code by adding features like interactive dialogues, command parsing, and more complex game mechanics.
Let me know if you need further assistance or have any questions!