SimpleGFX  1.0
Simple Graphics Library for C Programming Education
simplegfx.c File Reference

Implementation of the simple graphics library. More...

#include "simplegfx.h"
#include <string.h>
#include <windows.h>

Functions

void gfx_circle (int cx, int cy, int r)
 Draw a circle outline. More...
 
void gfx_clear ()
 Clear the entire graphics window to white. More...
 
void gfx_close ()
 Close the graphics window and free resources. More...
 
void gfx_delay (int ms)
 Pause program execution for specified milliseconds. More...
 
void gfx_drawtext (int x, int y, const char *text)
 Draw text at the specified position. More...
 
int gfx_init (int w, int h)
 Initialize the graphics window with specified dimensions. More...
 
void gfx_line (int x1, int y1, int x2, int y2)
 Draw a line between two points. More...
 
void gfx_present ()
 Display all drawn graphics on the screen. More...
 
void gfx_putpixel (int x, int y)
 Draw a single black pixel at the specified coordinates. More...
 
void gfx_putpixel_color (int x, int y, unsigned char r, unsigned char g, unsigned char b)
 Draw a single pixel with custom RGB color. More...
 
void gfx_rect (int x, int y, int w, int h)
 Draw a rectangle outline. More...
 
static LRESULT CALLBACK WndProc (HWND h, UINT m, WPARAM w, LPARAM l)
 

Variables

static HBITMAP backBmp
 
static HDC backDC
 
static HWND hwnd
 
static int winH
 
static int winW
 

Detailed Description

Implementation of the simple graphics library.

Author
Sukesh Ashok Kumar

Cross-platform graphics implementation:

  • Windows: Uses GDI (Graphics Device Interface) with double buffering
  • Linux: Uses X11 with optimized TrueColor pixel operations

The library automatically compiles the correct version based on platform.

Function Documentation

◆ gfx_circle()

void gfx_circle ( int  cx,
int  cy,
int  r 
)

Draw a circle outline.

Draws only the border of a circle, not filled.

Parameters
cxX-coordinate of the circle center
cyY-coordinate of the circle center
rRadius of the circle in pixels
gfx_circle(320, 240, 100); // Circle at center with radius 100
void gfx_circle(int cx, int cy, int r)
Draw a circle outline.
Definition: simplegfx.c:96

◆ gfx_clear()

void gfx_clear ( )

Clear the entire graphics window to white.

Erases everything on the screen and fills it with white color.

◆ gfx_close()

void gfx_close ( )

Close the graphics window and free resources.

Call this before your program exits to properly clean up. Should be the last graphics function you call.

return 0;
void gfx_close()
Close the graphics window and free resources.
Definition: simplegfx.c:120

◆ gfx_delay()

void gfx_delay ( int  ms)

Pause program execution for specified milliseconds.

Useful for keeping the window open or creating animations.

Parameters
msDelay duration in milliseconds (1000 ms = 1 second)
gfx_delay(3000); // Wait 3 seconds
void gfx_delay(int ms)
Pause program execution for specified milliseconds.
Definition: simplegfx.c:115

◆ gfx_drawtext()

void gfx_drawtext ( int  x,
int  y,
const char *  text 
)

Draw text at the specified position.

Renders text using the default system font in black color.

Parameters
xX-coordinate where text starts
yY-coordinate of the text baseline
textThe null-terminated string to draw
gfx_drawtext(50, 50, "Hello, World!");
void gfx_drawtext(int x, int y, const char *text)
Draw text at the specified position.
Definition: simplegfx.c:101

◆ gfx_init()

int gfx_init ( int  width,
int  height 
)

Initialize the graphics window with specified dimensions.

Creates a graphics window where you can draw shapes and pixels. Must be called before any other graphics functions.

Parameters
widthWindow width in pixels
heightWindow height in pixels
Returns
0 on success, -1 on failure (display unavailable, etc.)
if (gfx_init(640, 480) != 0) {
return 1; // Failed to create window
}
int gfx_init(int width, int height)
Initialize the graphics window with specified dimensions.
Definition: simplegfx.c:34

◆ gfx_line()

void gfx_line ( int  x1,
int  y1,
int  x2,
int  y2 
)

Draw a line between two points.

Draws a straight line connecting point (x1, y1) to point (x2, y2).

Parameters
x1X-coordinate of the start point
y1Y-coordinate of the start point
x2X-coordinate of the end point
y2Y-coordinate of the end point
gfx_line(0, 0, 100, 100); // Diagonal line
void gfx_line(int x1, int y1, int x2, int y2)
Draw a line between two points.
Definition: simplegfx.c:85

◆ gfx_present()

void gfx_present ( )

Display all drawn graphics on the screen.

Call this after drawing operations to make them visible. This updates the window with everything you've drawn since the last present.

gfx_circle(100, 100, 50);
gfx_present(); // Now the circle appears on screen
void gfx_present()
Display all drawn graphics on the screen.
Definition: simplegfx.c:108

◆ gfx_putpixel()

void gfx_putpixel ( int  x,
int  y 
)

Draw a single black pixel at the specified coordinates.

Parameters
xX-coordinate of the pixel (0 = left edge)
yY-coordinate of the pixel (0 = top edge)
gfx_putpixel(100, 50); // Draw black pixel at (100, 50)
void gfx_putpixel(int x, int y)
Draw a single black pixel at the specified coordinates.
Definition: simplegfx.c:72

◆ gfx_putpixel_color()

void gfx_putpixel_color ( int  x,
int  y,
unsigned char  r,
unsigned char  g,
unsigned char  b 
)

Draw a single pixel with custom RGB color.

Allows you to specify any color using Red, Green, Blue components. Each component ranges from 0 (none) to 255 (full intensity).

Parameters
xX-coordinate of the pixel
yY-coordinate of the pixel
rRed component (0-255)
gGreen component (0-255)
bBlue component (0-255)
gfx_putpixel_color(100, 50, 255, 0, 0); // Red pixel
gfx_putpixel_color(100, 51, 0, 255, 0); // Green pixel
gfx_putpixel_color(100, 52, 0, 0, 255); // Blue pixel
void gfx_putpixel_color(int x, int y, unsigned char r, unsigned char g, unsigned char b)
Draw a single pixel with custom RGB color.
Definition: simplegfx.c:77

◆ gfx_rect()

void gfx_rect ( int  x,
int  y,
int  w,
int  h 
)

Draw a rectangle outline.

Draws only the border of a rectangle, not filled.

Parameters
xX-coordinate of the top-left corner
yY-coordinate of the top-left corner
wWidth of the rectangle in pixels
hHeight of the rectangle in pixels
gfx_rect(50, 50, 200, 100); // 200x100 rectangle at (50, 50)
void gfx_rect(int x, int y, int w, int h)
Draw a rectangle outline.
Definition: simplegfx.c:91

◆ WndProc()

static LRESULT CALLBACK WndProc ( HWND  h,
UINT  m,
WPARAM  w,
LPARAM  l 
)
static

Variable Documentation

◆ backBmp

HBITMAP backBmp
static

◆ backDC

HDC backDC
static

◆ hwnd

HWND hwnd
static

◆ winH

int winH
static

◆ winW

int winW
static