From fbc5c101783533f90a3053671de5314c2c6a1c1a Mon Sep 17 00:00:00 2001 From: dawidg81 Date: Fri, 13 Feb 2026 10:13:59 +0100 Subject: Directory cleanup --- .sample3.cpp.swp | Bin 20480 -> 0 bytes SDL2.dll | Bin 1911808 -> 0 bytes examples/sample1.cpp | 123 ++++++++++++++++++++++++++++ examples/sample2.cpp | 102 +++++++++++++++++++++++ examples/sample3.cpp | 130 ++++++++++++++++++++++++++++++ graphics.cpp | 222 --------------------------------------------------- graphics.h | 56 ------------- lib/SDL2.dll | Bin 0 -> 1911808 bytes lib/graphics.cpp | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/graphics.h | 56 +++++++++++++ sample1.cpp | 123 ---------------------------- sample2.cpp | 102 ----------------------- sample3.cpp | 130 ------------------------------ 13 files changed, 633 insertions(+), 633 deletions(-) delete mode 100644 .sample3.cpp.swp delete mode 100644 SDL2.dll create mode 100644 examples/sample1.cpp create mode 100644 examples/sample2.cpp create mode 100644 examples/sample3.cpp delete mode 100644 graphics.cpp delete mode 100644 graphics.h create mode 100644 lib/SDL2.dll create mode 100644 lib/graphics.cpp create mode 100644 lib/graphics.h delete mode 100644 sample1.cpp delete mode 100644 sample2.cpp delete mode 100644 sample3.cpp diff --git a/.sample3.cpp.swp b/.sample3.cpp.swp deleted file mode 100644 index 44c7e09..0000000 Binary files a/.sample3.cpp.swp and /dev/null differ diff --git a/SDL2.dll b/SDL2.dll deleted file mode 100644 index 332c4fa..0000000 Binary files a/SDL2.dll and /dev/null differ diff --git a/examples/sample1.cpp b/examples/sample1.cpp new file mode 100644 index 0000000..490b0a1 --- /dev/null +++ b/examples/sample1.cpp @@ -0,0 +1,123 @@ +#include "graphics.h" +#include + +int main(int argc, char* argv[]) { + // Create a window + WindowHandle* window = createWindow("TestApp", 800, 600); + + if (!window) { + return -1; + } + + // Animation variables + float time = 0.0f; + const float PI = 3.14159265359f; + + // Main loop + while (!windowShouldClose(window)) { + // Handle events + pollEvents(window); + + // Clear screen with a dark background + clearScreen(window, Color(0, 0, 0)); + + // Draw a grid + Color gridColor(255, 255, 255); + for (int x = 0; x < 800; x += 50) { + drawLine(window, x, 0, x, 600, gridColor); + } + for (int y = 0; y < 600; y += 50) { + drawLine(window, 0, y, 800, y, gridColor); + } + + // Draw some static shapes + drawFilledRectangle(window, 50, 50, 100, 80, Color(255, 100, 100)); + drawRectangle(window, 48, 48, 104, 84, Color(255, 150, 150)); + + drawFilledCircle(window, 250, 90, 40, Color(100, 255, 100)); + drawCircle(window, 250, 90, 42, Color(150, 255, 150)); + + // Animated rotating line + int centerX = 400; + int centerY = 300; + int lineLength = 150; + int x2 = centerX + static_cast(lineLength * cos(time)); + int y2 = centerY + static_cast(lineLength * sin(time)); + + drawLine(window, centerX, centerY, x2, y2, Color(255, 255, 100, 255)); + drawFilledCircle(window, centerX, centerY, 5, Color(255, 200, 0)); + + // Animated bouncing circle + int bounceX = 600; + int bounceY = 300 + static_cast(100 * sin(time * 2)); + drawFilledCircle(window, bounceX, bounceY, 30, Color(100, 200, 255)); + + int prevX = bounceX + bounceX + time * 20; + int prevY = 300 + static_cast(100 * sin((bounceX + time * 20) * 2)); + + for (int i = bounceX + time * 20; i > bounceX; i--) { + int x = bounceX + bounceX + time * 20 - i; + int y = 300 + static_cast(100 * sin(i * 2)); + // drawPixel(window, x, y, Color(255, 0, 0)); + drawLine(window, prevX, prevY, x, y, Color(255, 0, 0)); + prevX = x; + prevY = y; + } + + // Draw multiple small circles in a pattern + for (int i = 0; i < 8; i++) { + float angle = (time + i * PI / 4); + int px = 400 + static_cast(100 * cos(angle)); + int py = 300 + static_cast(100 * sin(angle)); + + Color particleColor( + static_cast(128 + 127 * sin(time + i)), + static_cast(128 + 127 * sin(time + i + PI * 2 / 3)), + static_cast(128 + 127 * sin(time + i + PI * 4 / 3)) + ); + + drawFilledCircle(window, px, py, 8, particleColor); + } + + // Draw some text-like pixel art (a simple "HI" pattern) + Color pixelColor(255, 255, 255); + // Letter H + for (int y = 0; y < 20; y++) { + drawPixel(window, 50, 500 + y, pixelColor); + drawPixel(window, 70, 500 + y, pixelColor); + } + for (int x = 50; x <= 70; x++) { + drawPixel(window, x, 510, pixelColor); + } + + // Letter I + for (int y = 0; y < 20; y++) { + drawPixel(window, 90, 500 + y, pixelColor); + } + for (int x = 85; x <= 95; x++) { + drawPixel(window, x, 500, pixelColor); + drawPixel(window, x, 519, pixelColor); + } + + // Draw a gradient effect using rectangles + for (int i = 0; i < 50; i++) { + uint8_t colorValue = static_cast(i * 5); + drawFilledRectangle(window, 650 + i * 3, 450, 3, 100, + Color(colorValue, 0, 255 - colorValue)); + } + + // Present the rendered frame + swapBuffers(window); + + // Update animation + time += 0.02f; + + // Small delay to control frame rate (~60 FPS) + delay(16); + } + + // Cleanup + destroyWindow(window); + + return 0; +} diff --git a/examples/sample2.cpp b/examples/sample2.cpp new file mode 100644 index 0000000..02e1718 --- /dev/null +++ b/examples/sample2.cpp @@ -0,0 +1,102 @@ +#include "graphics.h" +#include +#include + +struct Vec3 { + float x, y, z; +}; + +// Rotate point around X axis +Vec3 rotateX(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { + v.x, + v.y * c - v.z * s, + v.y * s + v.z * c + }; +} + +// Rotate point around Y axis +Vec3 rotateY(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { + v.x * c + v.z * s, + v.y, + -v.x * s + v.z * c + }; +} + +// Perspective projection +void project(const Vec3& v, int& x2d, int& y2d, float width, float height) { + float distance = 2.0f; // camera distance + float scale = 100.0f; // zoom factor + + float z = v.z + distance; + float factor = scale / z; + + x2d = static_cast(v.x * factor + width / 2); + y2d = static_cast(v.y * factor + height / 2); +} + +int main() { + const int width = 640; + const int height = 480; + + WindowHandle* window = createWindow("3D Spinning Cube", width, height); + if (!window) return -1; + + // Cube vertices + std::vector vertices = { + {-1, -1, -1}, + { 1, -1, -1}, + { 1, 1, -1}, + {-1, 1, -1}, + {-1, -1, 1}, + { 1, -1, 1}, + { 1, 1, 1}, + {-1, 1, 1} + }; + + // Cube edges (pairs of vertex indices) + std::vector> edges = { + {0,1},{1,2},{2,3},{3,0}, // back face + {4,5},{5,6},{6,7},{7,4}, // front face + {0,4},{1,5},{2,6},{3,7} // connecting edges + }; + + float angle = 0.0f; + + while (!windowShouldClose(window)) { + pollEvents(window); + clearScreen(window, Color(0, 0, 0)); // RGB 20, 20, 30 + + std::vector transformed; + + // Rotate cube + for (const auto& v : vertices) { + Vec3 r = rotateX(v, angle); + r = rotateY(r, angle * 0.7f); + transformed.push_back(r); + } + + // Draw edges + for (const auto& edge : edges) { + int x1, y1, x2, y2; + + project(transformed[edge.first], x1, y1, width, height); + project(transformed[edge.second], x2, y2, width, height); + + drawLine(window, x1, y1, x2, y2, Color(255, 255, 255)); // RGB 0 255 180 + } + + swapBuffers(window); + + angle += 0.01f; + delay(16); // ~60 FPS + } + + destroyWindow(window); + return 0; +} diff --git a/examples/sample3.cpp b/examples/sample3.cpp new file mode 100644 index 0000000..70c1e13 --- /dev/null +++ b/examples/sample3.cpp @@ -0,0 +1,130 @@ +#include "graphics.h" +#include +#include +#include + +struct Vec3 { + float x, y, z; +}; + +Vec3 rotateX(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { v.x, v.y * c - v.z * s, v.y * s + v.z * c }; +} + +Vec3 rotateY(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { v.x * c + v.z * s, v.y, -v.x * s + v.z * c }; +} + +Vec3 rotateZ(const Vec3& v, float angle) { + float c = std::cos(angle); + float s = std::sin(angle); + return { v.x * c - v.y * s, v.x * s + v.y * c, v.z }; +} + +void project(const Vec3& v, int& x2d, int& y2d, float width, float height, float zoom) { + float distance = 4.0f; + float z = v.z + distance; + float factor = zoom / z; + + x2d = static_cast(v.x * factor + width / 2); + y2d = static_cast(v.y * factor + height / 2); +} + +struct Star { + float x, y, z; +}; + +int main() { + const int width = 800; + const int height = 600; + + WindowHandle* window = createWindow("DEMOSCENE CUBE", width, height); + if (!window) return -1; + + std::vector vertices = { + {-1,-1,-1},{1,-1,-1},{1,1,-1},{-1,1,-1}, + {-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1} + }; + + std::vector> edges = { + {0,1},{1,2},{2,3},{3,0}, + {4,5},{5,6},{6,7},{7,4}, + {0,4},{1,5},{2,6},{3,7} + }; + + // Starfield + const int starCount = 300; + std::vector stars; + for (int i = 0; i < starCount; ++i) { + stars.push_back({ + ((rand() % 200) - 100) / 10.0f, + ((rand() % 200) - 100) / 10.0f, + (rand() % 100) / 10.0f + }); + } + + float time = 0.0f; + + while (!windowShouldClose(window)) { + pollEvents(window); + + // Pulsing background + int bg = static_cast((std::sin(time * 0.5f) + 1.0f) * 20); + clearScreen(window, Color(bg, 0, bg + 20)); + + // --- STARFIELD --- + for (auto& star : stars) { + star.z -= 0.05f; + if (star.z <= 0.1f) + star.z = 10.0f; + + int sx = static_cast((star.x / star.z) * 200 + width/2); + int sy = static_cast((star.y / star.z) * 200 + height/2); + + if (sx >= 0 && sx < width && sy >= 0 && sy < height) + drawPixel(window, sx, sy, Color(255,255,255)); + } + + // Color cycling (rainbow) + int r = static_cast((std::sin(time) + 1) * 127); + int g = static_cast((std::sin(time + 2) + 1) * 127); + int b = static_cast((std::sin(time + 4) + 1) * 127); + + float zoom = 150.0f + std::sin(time) * 50.0f; + + // --- MULTI-CUBE EFFECT --- + for (int c = 0; c < 3; ++c) { + + float offset = c * 2.5f - 2.5f; + + std::vector transformed; + + for (const auto& v : vertices) { + Vec3 r3 = rotateX(v, time + c); + r3 = rotateY(r3, time * 0.7f + c); + r3 = rotateZ(r3, time * 0.5f); + r3.x += offset; + transformed.push_back(r3); + } + + for (const auto& edge : edges) { + int x1, y1, x2, y2; + project(transformed[edge.first], x1, y1, width, height, zoom); + project(transformed[edge.second], x2, y2, width, height, zoom); + drawLine(window, x1, y1, x2, y2, Color(r, g, b)); + } + } + + swapBuffers(window); + + time += 0.02f; + delay(16); + } + + destroyWindow(window); + return 0; +} diff --git a/graphics.cpp b/graphics.cpp deleted file mode 100644 index 827303e..0000000 --- a/graphics.cpp +++ /dev/null @@ -1,222 +0,0 @@ -#include "graphics.h" -#include - -#ifdef USE_SDL - -#include - -// Platform-specific window handle structure -struct WindowHandle { - SDL_Window* window; - SDL_Renderer* renderer; - bool shouldClose; - - WindowHandle() : window(nullptr), renderer(nullptr), shouldClose(false) {} -}; - -// Initialize SDL (called once) -static bool sdlInitialized = false; - -static void ensureSDLInit() { - if (!sdlInitialized) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) { - // Handle error - in production, use proper error handling - return; - } - sdlInitialized = true; - } -} - -WindowHandle* createWindow(const char* title, int width, int height) { - ensureSDLInit(); - - WindowHandle* handle = new WindowHandle(); - - handle->window = SDL_CreateWindow( - title, - SDL_WINDOWPOS_CENTERED, - SDL_WINDOWPOS_CENTERED, - width, - height, - SDL_WINDOW_SHOWN - ); - - if (!handle->window) { - delete handle; - return nullptr; - } - - handle->renderer = SDL_CreateRenderer( - handle->window, - -1, - SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC - ); - - if (!handle->renderer) { - SDL_DestroyWindow(handle->window); - delete handle; - return nullptr; - } - - // Enable alpha blending - SDL_SetRenderDrawBlendMode(handle->renderer, SDL_BLENDMODE_BLEND); - - return handle; -} - -void destroyWindow(WindowHandle* window) { - if (!window) return; - - if (window->renderer) { - SDL_DestroyRenderer(window->renderer); - } - - if (window->window) { - SDL_DestroyWindow(window->window); - } - - delete window; - - // Cleanup SDL if this was the last window - if (sdlInitialized) { - SDL_Quit(); - sdlInitialized = false; - } -} - -bool windowShouldClose(WindowHandle* window) { - if (!window) return true; - return window->shouldClose; -} - -void pollEvents(WindowHandle* window) { - if (!window) return; - - SDL_Event event; - while (SDL_PollEvent(&event)) { - if (event.type == SDL_QUIT) { - window->shouldClose = true; - } - else if (event.type == SDL_KEYDOWN) { - if (event.key.keysym.sym == SDLK_ESCAPE) { - window->shouldClose = true; - } - } - } -} - -void swapBuffers(WindowHandle* window) { - if (!window || !window->renderer) return; - SDL_RenderPresent(window->renderer); -} - -void clearScreen(WindowHandle* window, const Color& color) { - if (!window || !window->renderer) return; - - SDL_SetRenderDrawColor(window->renderer, color.r, color.g, color.b, color.a); - SDL_RenderClear(window->renderer); -} - -void setDrawColor(WindowHandle* window, const Color& color) { - if (!window || !window->renderer) return; - SDL_SetRenderDrawColor(window->renderer, color.r, color.g, color.b, color.a); -} - -void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color) { - if (!window || !window->renderer) return; - - setDrawColor(window, color); - SDL_RenderDrawLine(window->renderer, x1, y1, x2, y2); -} - -void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) { - if (!window || !window->renderer) return; - - SDL_Rect rect = {x, y, width, height}; - setDrawColor(window, color); - SDL_RenderDrawRect(window->renderer, &rect); -} - -void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) { - if (!window || !window->renderer) return; - - SDL_Rect rect = {x, y, width, height}; - setDrawColor(window, color); - SDL_RenderFillRect(window->renderer, &rect); -} - -void drawPixel(WindowHandle* window, int x, int y, const Color& color) { - if (!window || !window->renderer) return; - - setDrawColor(window, color); - SDL_RenderDrawPoint(window->renderer, x, y); -} - -// Helper function for drawing circles using midpoint circle algorithm -void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) { - if (!window || !window->renderer) return; - - setDrawColor(window, color); - - int x = 0; - int y = radius; - int d = 3 - 2 * radius; - - auto drawCirclePoints = [&](int xc, int yc, int x, int y) { - SDL_RenderDrawPoint(window->renderer, xc + x, yc + y); - SDL_RenderDrawPoint(window->renderer, xc - x, yc + y); - SDL_RenderDrawPoint(window->renderer, xc + x, yc - y); - SDL_RenderDrawPoint(window->renderer, xc - x, yc - y); - SDL_RenderDrawPoint(window->renderer, xc + y, yc + x); - SDL_RenderDrawPoint(window->renderer, xc - y, yc + x); - SDL_RenderDrawPoint(window->renderer, xc + y, yc - x); - SDL_RenderDrawPoint(window->renderer, xc - y, yc - x); - }; - - drawCirclePoints(centerX, centerY, x, y); - - while (y >= x) { - x++; - - if (d > 0) { - y--; - d = d + 4 * (x - y) + 10; - } else { - d = d + 4 * x + 6; - } - - drawCirclePoints(centerX, centerY, x, y); - } -} - -void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) { - if (!window || !window->renderer) return; - - setDrawColor(window, color); - - for (int y = -radius; y <= radius; y++) { - for (int x = -radius; x <= radius; x++) { - if (x * x + y * y <= radius * radius) { - SDL_RenderDrawPoint(window->renderer, centerX + x, centerY + y); - } - } - } -} - -void delay(uint32_t milliseconds) { - SDL_Delay(milliseconds); -} - -#endif // USE_SDL - -#ifdef USE_WIN32 -// Win32 implementation would go here -// For now, this is a placeholder -#error "Win32 backend not yet implemented" -#endif - -#ifdef USE_X11 -// X11 implementation would go here -// For now, this is a placeholder -#error "X11 backend not yet implemented" -#endif \ No newline at end of file diff --git a/graphics.h b/graphics.h deleted file mode 100644 index cc0bd63..0000000 --- a/graphics.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef GRAPHICS_H -#define GRAPHICS_H - -#include - -// Platform detection -#if defined(_WIN32) || defined(_WIN64) - #define PLATFORM_WINDOWS -#elif defined(__linux__) - #define PLATFORM_LINUX -#elif defined(__APPLE__) - #define PLATFORM_MACOS -#endif - -// Backend selection -// Define one of these before including this header, or let it auto-detect -#if !defined(USE_SDL) && !defined(USE_WIN32) && !defined(USE_X11) - #ifdef PLATFORM_WINDOWS - #define USE_SDL // Default to SDL on Windows for now - #elif defined(PLATFORM_LINUX) - #define USE_X11 - #endif -#endif - -// Color structure -struct Color { - uint8_t r, g, b, a; - - Color(uint8_t red = 0, uint8_t green = 0, uint8_t blue = 0, uint8_t alpha = 255) - : r(red), g(green), b(blue), a(alpha) {} -}; - -// Forward declarations for platform-specific types -struct WindowHandle; - -// Window management functions -WindowHandle* createWindow(const char* title, int width, int height); -void destroyWindow(WindowHandle* window); -bool windowShouldClose(WindowHandle* window); -void pollEvents(WindowHandle* window); -void swapBuffers(WindowHandle* window); - -// Drawing functions -void clearScreen(WindowHandle* window, const Color& color); -void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color); -void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color); -void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color); -void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color); -void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color); -void drawPixel(WindowHandle* window, int x, int y, const Color& color); - -// Utility functions -void setDrawColor(WindowHandle* window, const Color& color); -void delay(uint32_t milliseconds); - -#endif // GRAPHICS_H \ No newline at end of file diff --git a/lib/SDL2.dll b/lib/SDL2.dll new file mode 100644 index 0000000..332c4fa Binary files /dev/null and b/lib/SDL2.dll differ diff --git a/lib/graphics.cpp b/lib/graphics.cpp new file mode 100644 index 0000000..827303e --- /dev/null +++ b/lib/graphics.cpp @@ -0,0 +1,222 @@ +#include "graphics.h" +#include + +#ifdef USE_SDL + +#include + +// Platform-specific window handle structure +struct WindowHandle { + SDL_Window* window; + SDL_Renderer* renderer; + bool shouldClose; + + WindowHandle() : window(nullptr), renderer(nullptr), shouldClose(false) {} +}; + +// Initialize SDL (called once) +static bool sdlInitialized = false; + +static void ensureSDLInit() { + if (!sdlInitialized) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + // Handle error - in production, use proper error handling + return; + } + sdlInitialized = true; + } +} + +WindowHandle* createWindow(const char* title, int width, int height) { + ensureSDLInit(); + + WindowHandle* handle = new WindowHandle(); + + handle->window = SDL_CreateWindow( + title, + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + width, + height, + SDL_WINDOW_SHOWN + ); + + if (!handle->window) { + delete handle; + return nullptr; + } + + handle->renderer = SDL_CreateRenderer( + handle->window, + -1, + SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC + ); + + if (!handle->renderer) { + SDL_DestroyWindow(handle->window); + delete handle; + return nullptr; + } + + // Enable alpha blending + SDL_SetRenderDrawBlendMode(handle->renderer, SDL_BLENDMODE_BLEND); + + return handle; +} + +void destroyWindow(WindowHandle* window) { + if (!window) return; + + if (window->renderer) { + SDL_DestroyRenderer(window->renderer); + } + + if (window->window) { + SDL_DestroyWindow(window->window); + } + + delete window; + + // Cleanup SDL if this was the last window + if (sdlInitialized) { + SDL_Quit(); + sdlInitialized = false; + } +} + +bool windowShouldClose(WindowHandle* window) { + if (!window) return true; + return window->shouldClose; +} + +void pollEvents(WindowHandle* window) { + if (!window) return; + + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + window->shouldClose = true; + } + else if (event.type == SDL_KEYDOWN) { + if (event.key.keysym.sym == SDLK_ESCAPE) { + window->shouldClose = true; + } + } + } +} + +void swapBuffers(WindowHandle* window) { + if (!window || !window->renderer) return; + SDL_RenderPresent(window->renderer); +} + +void clearScreen(WindowHandle* window, const Color& color) { + if (!window || !window->renderer) return; + + SDL_SetRenderDrawColor(window->renderer, color.r, color.g, color.b, color.a); + SDL_RenderClear(window->renderer); +} + +void setDrawColor(WindowHandle* window, const Color& color) { + if (!window || !window->renderer) return; + SDL_SetRenderDrawColor(window->renderer, color.r, color.g, color.b, color.a); +} + +void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color) { + if (!window || !window->renderer) return; + + setDrawColor(window, color); + SDL_RenderDrawLine(window->renderer, x1, y1, x2, y2); +} + +void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) { + if (!window || !window->renderer) return; + + SDL_Rect rect = {x, y, width, height}; + setDrawColor(window, color); + SDL_RenderDrawRect(window->renderer, &rect); +} + +void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) { + if (!window || !window->renderer) return; + + SDL_Rect rect = {x, y, width, height}; + setDrawColor(window, color); + SDL_RenderFillRect(window->renderer, &rect); +} + +void drawPixel(WindowHandle* window, int x, int y, const Color& color) { + if (!window || !window->renderer) return; + + setDrawColor(window, color); + SDL_RenderDrawPoint(window->renderer, x, y); +} + +// Helper function for drawing circles using midpoint circle algorithm +void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) { + if (!window || !window->renderer) return; + + setDrawColor(window, color); + + int x = 0; + int y = radius; + int d = 3 - 2 * radius; + + auto drawCirclePoints = [&](int xc, int yc, int x, int y) { + SDL_RenderDrawPoint(window->renderer, xc + x, yc + y); + SDL_RenderDrawPoint(window->renderer, xc - x, yc + y); + SDL_RenderDrawPoint(window->renderer, xc + x, yc - y); + SDL_RenderDrawPoint(window->renderer, xc - x, yc - y); + SDL_RenderDrawPoint(window->renderer, xc + y, yc + x); + SDL_RenderDrawPoint(window->renderer, xc - y, yc + x); + SDL_RenderDrawPoint(window->renderer, xc + y, yc - x); + SDL_RenderDrawPoint(window->renderer, xc - y, yc - x); + }; + + drawCirclePoints(centerX, centerY, x, y); + + while (y >= x) { + x++; + + if (d > 0) { + y--; + d = d + 4 * (x - y) + 10; + } else { + d = d + 4 * x + 6; + } + + drawCirclePoints(centerX, centerY, x, y); + } +} + +void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) { + if (!window || !window->renderer) return; + + setDrawColor(window, color); + + for (int y = -radius; y <= radius; y++) { + for (int x = -radius; x <= radius; x++) { + if (x * x + y * y <= radius * radius) { + SDL_RenderDrawPoint(window->renderer, centerX + x, centerY + y); + } + } + } +} + +void delay(uint32_t milliseconds) { + SDL_Delay(milliseconds); +} + +#endif // USE_SDL + +#ifdef USE_WIN32 +// Win32 implementation would go here +// For now, this is a placeholder +#error "Win32 backend not yet implemented" +#endif + +#ifdef USE_X11 +// X11 implementation would go here +// For now, this is a placeholder +#error "X11 backend not yet implemented" +#endif \ No newline at end of file diff --git a/lib/graphics.h b/lib/graphics.h new file mode 100644 index 0000000..cc0bd63 --- /dev/null +++ b/lib/graphics.h @@ -0,0 +1,56 @@ +#ifndef GRAPHICS_H +#define GRAPHICS_H + +#include + +// Platform detection +#if defined(_WIN32) || defined(_WIN64) + #define PLATFORM_WINDOWS +#elif defined(__linux__) + #define PLATFORM_LINUX +#elif defined(__APPLE__) + #define PLATFORM_MACOS +#endif + +// Backend selection +// Define one of these before including this header, or let it auto-detect +#if !defined(USE_SDL) && !defined(USE_WIN32) && !defined(USE_X11) + #ifdef PLATFORM_WINDOWS + #define USE_SDL // Default to SDL on Windows for now + #elif defined(PLATFORM_LINUX) + #define USE_X11 + #endif +#endif + +// Color structure +struct Color { + uint8_t r, g, b, a; + + Color(uint8_t red = 0, uint8_t green = 0, uint8_t blue = 0, uint8_t alpha = 255) + : r(red), g(green), b(blue), a(alpha) {} +}; + +// Forward declarations for platform-specific types +struct WindowHandle; + +// Window management functions +WindowHandle* createWindow(const char* title, int width, int height); +void destroyWindow(WindowHandle* window); +bool windowShouldClose(WindowHandle* window); +void pollEvents(WindowHandle* window); +void swapBuffers(WindowHandle* window); + +// Drawing functions +void clearScreen(WindowHandle* window, const Color& color); +void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color); +void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color); +void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color); +void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color); +void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color); +void drawPixel(WindowHandle* window, int x, int y, const Color& color); + +// Utility functions +void setDrawColor(WindowHandle* window, const Color& color); +void delay(uint32_t milliseconds); + +#endif // GRAPHICS_H \ No newline at end of file diff --git a/sample1.cpp b/sample1.cpp deleted file mode 100644 index 490b0a1..0000000 --- a/sample1.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include "graphics.h" -#include - -int main(int argc, char* argv[]) { - // Create a window - WindowHandle* window = createWindow("TestApp", 800, 600); - - if (!window) { - return -1; - } - - // Animation variables - float time = 0.0f; - const float PI = 3.14159265359f; - - // Main loop - while (!windowShouldClose(window)) { - // Handle events - pollEvents(window); - - // Clear screen with a dark background - clearScreen(window, Color(0, 0, 0)); - - // Draw a grid - Color gridColor(255, 255, 255); - for (int x = 0; x < 800; x += 50) { - drawLine(window, x, 0, x, 600, gridColor); - } - for (int y = 0; y < 600; y += 50) { - drawLine(window, 0, y, 800, y, gridColor); - } - - // Draw some static shapes - drawFilledRectangle(window, 50, 50, 100, 80, Color(255, 100, 100)); - drawRectangle(window, 48, 48, 104, 84, Color(255, 150, 150)); - - drawFilledCircle(window, 250, 90, 40, Color(100, 255, 100)); - drawCircle(window, 250, 90, 42, Color(150, 255, 150)); - - // Animated rotating line - int centerX = 400; - int centerY = 300; - int lineLength = 150; - int x2 = centerX + static_cast(lineLength * cos(time)); - int y2 = centerY + static_cast(lineLength * sin(time)); - - drawLine(window, centerX, centerY, x2, y2, Color(255, 255, 100, 255)); - drawFilledCircle(window, centerX, centerY, 5, Color(255, 200, 0)); - - // Animated bouncing circle - int bounceX = 600; - int bounceY = 300 + static_cast(100 * sin(time * 2)); - drawFilledCircle(window, bounceX, bounceY, 30, Color(100, 200, 255)); - - int prevX = bounceX + bounceX + time * 20; - int prevY = 300 + static_cast(100 * sin((bounceX + time * 20) * 2)); - - for (int i = bounceX + time * 20; i > bounceX; i--) { - int x = bounceX + bounceX + time * 20 - i; - int y = 300 + static_cast(100 * sin(i * 2)); - // drawPixel(window, x, y, Color(255, 0, 0)); - drawLine(window, prevX, prevY, x, y, Color(255, 0, 0)); - prevX = x; - prevY = y; - } - - // Draw multiple small circles in a pattern - for (int i = 0; i < 8; i++) { - float angle = (time + i * PI / 4); - int px = 400 + static_cast(100 * cos(angle)); - int py = 300 + static_cast(100 * sin(angle)); - - Color particleColor( - static_cast(128 + 127 * sin(time + i)), - static_cast(128 + 127 * sin(time + i + PI * 2 / 3)), - static_cast(128 + 127 * sin(time + i + PI * 4 / 3)) - ); - - drawFilledCircle(window, px, py, 8, particleColor); - } - - // Draw some text-like pixel art (a simple "HI" pattern) - Color pixelColor(255, 255, 255); - // Letter H - for (int y = 0; y < 20; y++) { - drawPixel(window, 50, 500 + y, pixelColor); - drawPixel(window, 70, 500 + y, pixelColor); - } - for (int x = 50; x <= 70; x++) { - drawPixel(window, x, 510, pixelColor); - } - - // Letter I - for (int y = 0; y < 20; y++) { - drawPixel(window, 90, 500 + y, pixelColor); - } - for (int x = 85; x <= 95; x++) { - drawPixel(window, x, 500, pixelColor); - drawPixel(window, x, 519, pixelColor); - } - - // Draw a gradient effect using rectangles - for (int i = 0; i < 50; i++) { - uint8_t colorValue = static_cast(i * 5); - drawFilledRectangle(window, 650 + i * 3, 450, 3, 100, - Color(colorValue, 0, 255 - colorValue)); - } - - // Present the rendered frame - swapBuffers(window); - - // Update animation - time += 0.02f; - - // Small delay to control frame rate (~60 FPS) - delay(16); - } - - // Cleanup - destroyWindow(window); - - return 0; -} diff --git a/sample2.cpp b/sample2.cpp deleted file mode 100644 index 02e1718..0000000 --- a/sample2.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "graphics.h" -#include -#include - -struct Vec3 { - float x, y, z; -}; - -// Rotate point around X axis -Vec3 rotateX(const Vec3& v, float angle) { - float c = std::cos(angle); - float s = std::sin(angle); - return { - v.x, - v.y * c - v.z * s, - v.y * s + v.z * c - }; -} - -// Rotate point around Y axis -Vec3 rotateY(const Vec3& v, float angle) { - float c = std::cos(angle); - float s = std::sin(angle); - return { - v.x * c + v.z * s, - v.y, - -v.x * s + v.z * c - }; -} - -// Perspective projection -void project(const Vec3& v, int& x2d, int& y2d, float width, float height) { - float distance = 2.0f; // camera distance - float scale = 100.0f; // zoom factor - - float z = v.z + distance; - float factor = scale / z; - - x2d = static_cast(v.x * factor + width / 2); - y2d = static_cast(v.y * factor + height / 2); -} - -int main() { - const int width = 640; - const int height = 480; - - WindowHandle* window = createWindow("3D Spinning Cube", width, height); - if (!window) return -1; - - // Cube vertices - std::vector vertices = { - {-1, -1, -1}, - { 1, -1, -1}, - { 1, 1, -1}, - {-1, 1, -1}, - {-1, -1, 1}, - { 1, -1, 1}, - { 1, 1, 1}, - {-1, 1, 1} - }; - - // Cube edges (pairs of vertex indices) - std::vector> edges = { - {0,1},{1,2},{2,3},{3,0}, // back face - {4,5},{5,6},{6,7},{7,4}, // front face - {0,4},{1,5},{2,6},{3,7} // connecting edges - }; - - float angle = 0.0f; - - while (!windowShouldClose(window)) { - pollEvents(window); - clearScreen(window, Color(0, 0, 0)); // RGB 20, 20, 30 - - std::vector transformed; - - // Rotate cube - for (const auto& v : vertices) { - Vec3 r = rotateX(v, angle); - r = rotateY(r, angle * 0.7f); - transformed.push_back(r); - } - - // Draw edges - for (const auto& edge : edges) { - int x1, y1, x2, y2; - - project(transformed[edge.first], x1, y1, width, height); - project(transformed[edge.second], x2, y2, width, height); - - drawLine(window, x1, y1, x2, y2, Color(255, 255, 255)); // RGB 0 255 180 - } - - swapBuffers(window); - - angle += 0.01f; - delay(16); // ~60 FPS - } - - destroyWindow(window); - return 0; -} diff --git a/sample3.cpp b/sample3.cpp deleted file mode 100644 index 70c1e13..0000000 --- a/sample3.cpp +++ /dev/null @@ -1,130 +0,0 @@ -#include "graphics.h" -#include -#include -#include - -struct Vec3 { - float x, y, z; -}; - -Vec3 rotateX(const Vec3& v, float angle) { - float c = std::cos(angle); - float s = std::sin(angle); - return { v.x, v.y * c - v.z * s, v.y * s + v.z * c }; -} - -Vec3 rotateY(const Vec3& v, float angle) { - float c = std::cos(angle); - float s = std::sin(angle); - return { v.x * c + v.z * s, v.y, -v.x * s + v.z * c }; -} - -Vec3 rotateZ(const Vec3& v, float angle) { - float c = std::cos(angle); - float s = std::sin(angle); - return { v.x * c - v.y * s, v.x * s + v.y * c, v.z }; -} - -void project(const Vec3& v, int& x2d, int& y2d, float width, float height, float zoom) { - float distance = 4.0f; - float z = v.z + distance; - float factor = zoom / z; - - x2d = static_cast(v.x * factor + width / 2); - y2d = static_cast(v.y * factor + height / 2); -} - -struct Star { - float x, y, z; -}; - -int main() { - const int width = 800; - const int height = 600; - - WindowHandle* window = createWindow("DEMOSCENE CUBE", width, height); - if (!window) return -1; - - std::vector vertices = { - {-1,-1,-1},{1,-1,-1},{1,1,-1},{-1,1,-1}, - {-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1} - }; - - std::vector> edges = { - {0,1},{1,2},{2,3},{3,0}, - {4,5},{5,6},{6,7},{7,4}, - {0,4},{1,5},{2,6},{3,7} - }; - - // Starfield - const int starCount = 300; - std::vector stars; - for (int i = 0; i < starCount; ++i) { - stars.push_back({ - ((rand() % 200) - 100) / 10.0f, - ((rand() % 200) - 100) / 10.0f, - (rand() % 100) / 10.0f - }); - } - - float time = 0.0f; - - while (!windowShouldClose(window)) { - pollEvents(window); - - // Pulsing background - int bg = static_cast((std::sin(time * 0.5f) + 1.0f) * 20); - clearScreen(window, Color(bg, 0, bg + 20)); - - // --- STARFIELD --- - for (auto& star : stars) { - star.z -= 0.05f; - if (star.z <= 0.1f) - star.z = 10.0f; - - int sx = static_cast((star.x / star.z) * 200 + width/2); - int sy = static_cast((star.y / star.z) * 200 + height/2); - - if (sx >= 0 && sx < width && sy >= 0 && sy < height) - drawPixel(window, sx, sy, Color(255,255,255)); - } - - // Color cycling (rainbow) - int r = static_cast((std::sin(time) + 1) * 127); - int g = static_cast((std::sin(time + 2) + 1) * 127); - int b = static_cast((std::sin(time + 4) + 1) * 127); - - float zoom = 150.0f + std::sin(time) * 50.0f; - - // --- MULTI-CUBE EFFECT --- - for (int c = 0; c < 3; ++c) { - - float offset = c * 2.5f - 2.5f; - - std::vector transformed; - - for (const auto& v : vertices) { - Vec3 r3 = rotateX(v, time + c); - r3 = rotateY(r3, time * 0.7f + c); - r3 = rotateZ(r3, time * 0.5f); - r3.x += offset; - transformed.push_back(r3); - } - - for (const auto& edge : edges) { - int x1, y1, x2, y2; - project(transformed[edge.first], x1, y1, width, height, zoom); - project(transformed[edge.second], x2, y2, width, height, zoom); - drawLine(window, x1, y1, x2, y2, Color(r, g, b)); - } - } - - swapBuffers(window); - - time += 0.02f; - delay(16); - } - - destroyWindow(window); - return 0; -} -- cgit v1.2.3