raylib polygon program screenshots

Previously I had made a program with a spinning polygon that could be changed by keypresses using freeglut and glfw.

I am now making a raylib version of my polygon program. This one may not do all of the fancy things that my freeglut or glfw programs do because those have mountains of my own freaky code with sin and cos functions and storing arrays of points and use of of a function pointer to point to each of my several polygon drawing methods.

In this example, simplicity is the key. Raylib was intended to be a tool for teaching programming and it takes a lot less source code to get things working compared to my other methods. Here you can see I am calling the built in function for drawing a regular polygon. I also have a function which checks for keypresses 0 to 9 and if I press the right key I can change the color or number of sides of the polygon just like the glut/glfw versions.

It’s just barely small enough to fit all on one page but it works very well. Programming is much more exciting when I am drawing polygons to a screen than just outputting text.

#include "raylib.h"

const int width = 1280;
const int height = 720;
int polygon_sides=3;
double polygon_radius=height/2;
double polygon_radians=0;
Color polygon_color={255,255,255,255};

void keyboard()
{
 if(IsKeyPressed(KEY_ONE))   {polygon_color=(Color){  0,  0,255,255};}
 if(IsKeyPressed(KEY_TWO))   {polygon_color=(Color){  0,255,  0,255};}
 if(IsKeyPressed(KEY_THREE)) {polygon_color=(Color){  0,255,255,255};}
 if(IsKeyPressed(KEY_FOUR))  {polygon_color=(Color){255,  0,  0,255};}
 if(IsKeyPressed(KEY_FIVE))  {polygon_color=(Color){255,  0,255,255};}
 if(IsKeyPressed(KEY_SIX))   {polygon_color=(Color){255,255,  0,255};}
 if(IsKeyPressed(KEY_SEVEN)) {polygon_color=(Color){255,255,255,255};}

 if(IsKeyPressed(KEY_EIGHT)) {if(polygon_sides>3){polygon_sides-=1;}}
 if(IsKeyPressed(KEY_NINE))  {polygon_sides++;}
}

int main(int argc, char **argv)
{
 InitWindow(width,height,"Chastity's polygon using raylib");
 SetTargetFPS(60);
 while(!WindowShouldClose())
 {
  BeginDrawing();
  ClearBackground((Color){0,0,0,255});
  DrawPoly((Vector2){width/2,height/2}, polygon_sides, polygon_radius, polygon_radians,polygon_color);
  polygon_radians+=1;
  EndDrawing();
  keyboard();
 }
 CloseWindow();
 return 0;
}

/*
raylib_linux:
	gcc -Wall -std=c99 -pedantic main.c -o main -lraylib -lOpenGL -lm -lpthread -ldl -lrt -lX11 && ./main
raylib_windows:
	gcc -Wall -std=c99 -pedantic main.c -o main -I/usr/local/include -L/usr/local/lib -lraylib -lopengl32 -lgdi32 -lwinmm -lpthread -static && ./main
	strip main.exe
*/

The above should display the text form of the code. Below is what the screenshot looks like in Notepad++.

And here are some others of my compiling and running it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s