summaryrefslogtreecommitdiff
path: root/README.md
blob: 73493fc416db4be8a5e15be6903b24bfba8cf17d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Graffiklib

A simple cross-platform graphics abstraction layer with support for multiple backends (SDL2, Win32, X11).

## Current Implementation

Currently implemented backends:
- **SDL2** (Windows) - Fully implemented

Planned backends:
- **Win32** (Windows native) - Placeholder
- **X11** (Linux) - Placeholder

## Features

- Window creation and management
- Basic drawing primitives:
  - Lines
  - Rectangles (filled and outlined)
  - Circles (filled and outlined)
  - Pixels
- Color support with alpha channel
- Simple event handling
- Cross-platform delay function

## Building

### Windows (with SDL2)

#### Prerequisites
1. **C++ compiler**: MinGW or MSVC
2. **SDL2 development libraries**: Download from [libsdl.org](https://www.libsdl.org/download-2.0.php)
   - For MinGW: Download `SDL2-devel-2.x.x-mingw.tar.gz`
   - For MSVC: Download `SDL2-devel-2.x.x-VC.zip`

#### Installing SDL2

1. **Download SDL2** development libraries for your compiler
2. **Extract** the archive
3. **Place SDL2** in one of these locations:
   - `C:/SDL2`
   - `D:/SDL2`
   - Or in your project directory: `<project>/SDL2`

#### Option 1: Using Makefile (MinGW - Easiest)

```bash
# If SDL2 is in C:/SDL2
make

# If SDL2 is elsewhere
make SDL2_PATH=D:/path/to/SDL2

# Run the demo
make run
```

**Note**: Make sure `SDL2.dll` from `SDL2/bin` is in the same directory as the executable or in your PATH.

#### Option 2: Using CMake

```bash
mkdir build
cd build

# If SDL2 is in a standard location (C:/SDL2 or D:/SDL2)
cmake ..

# If SDL2 is elsewhere
cmake .. -DCMAKE_PREFIX_PATH=D:/path/to/SDL2

# Build
cmake --build .
```

The CMakeLists.txt will automatically copy SDL2.dll to the build directory.

#### Option 3: Manual compilation with g++/MinGW

```bash
# Adjust paths to match your SDL2 installation
g++ -std=c++11 -DUSE_SDL -o GraphicsDemo.exe main.cpp graphics.cpp ^
    -IC:/SDL2/include ^
    -LC:/SDL2/lib ^
    -lmingw32 -lSDL2main -lSDL2 -mwindows

# Copy SDL2.dll to the same directory
copy C:\SDL2\bin\SDL2.dll .
```

#### Option 4: Manual compilation with MSVC

```bash
cl /EHsc /DUSE_SDL main.cpp graphics.cpp ^
   /I"C:\SDL2\include" ^
   /link "C:\SDL2\lib\x64\SDL2.lib" "C:\SDL2\lib\x64\SDL2main.lib" ^
   /SUBSYSTEM:CONSOLE

# Copy SDL2.dll
copy C:\SDL2\lib\x64\SDL2.dll .
```

### Linux (X11 - Not yet implemented)
```bash
g++ -o demo main.cpp graphics.cpp -lX11 -DUSE_X11
```

## Usage Example

```cpp
#include "graphics.h"

int main() {
    // Create a window
    WindowHandle* window = createWindow("My Window", 800, 600);
    
    // Main loop
    while (!windowShouldClose(window)) {
        pollEvents(window);
        
        // Clear screen
        clearScreen(window, Color(0, 0, 0));
        
        // Draw shapes
        drawLine(window, 10, 10, 100, 100, Color(255, 0, 0));
        drawFilledCircle(window, 200, 200, 50, Color(0, 255, 0));
        drawRectangle(window, 300, 300, 100, 100, Color(0, 0, 255));
        
        // Present
        swapBuffers(window);
        
        delay(16); // ~60 FPS
    }
    
    // Cleanup
    destroyWindow(window);
    return 0;
}
```

## Architecture

The library uses a simple abstraction pattern:

1. **graphics.h** - Platform-independent API declarations
2. **graphics.cpp** - Platform-specific implementations (selected at compile time)
3. **Compile-time selection** - Backend is chosen via preprocessor defines

### Adding a New Backend

To add a new backend (e.g., Vulkan, DirectX):

1. Add a new `#ifdef USE_BACKEND` section in `graphics.cpp`
2. Implement all functions declared in `graphics.h`
3. Update the platform detection logic in `graphics.h` if needed
4. Add compilation instructions to this README

## API Reference

### Window Management
- `WindowHandle* createWindow(const char* title, int width, int height)` - Create a new window
- `void destroyWindow(WindowHandle* window)` - Destroy window and free resources
- `bool windowShouldClose(WindowHandle* window)` - Check if window should close
- `void pollEvents(WindowHandle* window)` - Process window events
- `void swapBuffers(WindowHandle* window)` - Present rendered frame

### Drawing Functions
- `void clearScreen(WindowHandle* window, const Color& color)` - Clear screen with color
- `void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color)` - Draw a line
- `void drawRectangle(WindowHandle* window, int x, int y, int w, int h, const Color& color)` - Draw rectangle outline
- `void drawFilledRectangle(WindowHandle* window, int x, int y, int w, int h, const Color& color)` - Draw filled rectangle
- `void drawCircle(WindowHandle* window, int cx, int cy, int radius, const Color& color)` - Draw circle outline
- `void drawFilledCircle(WindowHandle* window, int cx, int cy, int radius, const Color& color)` - Draw filled circle
- `void drawPixel(WindowHandle* window, int x, int y, const Color& color)` - Draw a single pixel

### Utility Functions
- `void setDrawColor(WindowHandle* window, const Color& color)` - Set current drawing color
- `void delay(uint32_t milliseconds)` - Delay execution

### Color Structure
```cpp
struct Color {
    uint8_t r, g, b, a;
    Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
};
```

## License

MIT License. See [LICENSE](LICENSE) file.