Blog

  • 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.

  • OpenGL Polygon

    While running on Linux I discovered a bug in my makefiles which caused nothing to compile if it did not have the “-lm” flag. This is because the sin and cos functions from the standard math library. Windows was implicitly adding it behind the scenes so I never learned to include it on the command line.


    But more importantly, I got a working OpenGL regular polygon program working. I spent hours going over the documentation of freeglut and opengl to figure out how to get it working. When I have had some rest I want to do a video covering that. Then eventually I’ll have to figure out how I get an accessible pixel array from a freeglut created window the same as I normally do with SDL. Once I have access to the pixels that means saving them with my BBM library code to files. This means a combination of my existing math which has proven to be great at creating animations but also the speed of OpenGL rendering polygon. There is still so much to learn but once I can draw a polygon I can always draw more. Squares are also polygons and so I can draw an whole checkerboard using opengl polygons once I take the time to code it.


    My OpenGL regular polygon source code is available here for anyone who wants to see.
    https://sourceforge.net/projects/binary-bit-map/files/source/opengl/gl_regpoly/

  • Learning OpenGL

    So far I managed to compile two of the tutorial lessons from lazyfoo.net.

    This is my makefile for compiling the first two OpenGL tutorials from lazyfoo.net:

    opengl:
    	gcc -Wall -ansi -pedantic LUtil.cpp main.cpp -o main -lOpenGL32 -lglu32 -lfreeglut && ./main
    opengl_static:
    	gcc -Wall -ansi -pedantic LUtil.cpp main.cpp -o main -D FREEGLUT_STATIC -lfreeglut_static -lwinmm -lgdi32 -lOpenGL32 -lglu32 && ./main

    Here are the links to the tutorials where I got the first two lessons.

    https://lazyfoo.net/tutorials/OpenGL/01_hello_opengl/index2.phphttps://lazyfoo.net/tutorials/OpenGL/02_matrices_and_coloring_polygons/index.php

    The source code of those tutorials is split into 4 files and lazyfoo is really picky about people not sharing the source code anywhere else. But as you can see from the following screenshot I got it working!



  • Programming without the Mouse using Vim

    I have achieved a major milestone. I can now work on my programming even when my mouse fails! Vim is a great text editor with syntax highlighting that also allows for running commands! In my case I use :!make to run the script that compiles and runs my code!

    My “Infinity Checkerboard” project has come very far from what it was. I can do incredible things as I learn more of the SDL library. Although this game does not contain much, the source code is very small and only requires the base SDL library with no extensions and a working C compiler.

    I hope that others benefit from this video both as an example of the power of SDL and the C programming language but also as an example of using Vim as a text editor.

  • Deleting Cygwin

    Now I know where all my disk space went! I have not been using cygwin ever since msys provided all my programming needs and can compile my SDL,Allegro, and SFML programs from a Unix environment just fine. I’m deleting cygwin to recover some disk space but still it was a very good Unix environment for what I used it for. I highly recommend programmers try out projects like msys and cygwin for that Unix/Linux feel if they’ve done programming on Linux and like the way it worked.

    https://www.msys2.org/
    https://cygwin.com/

    Also, there is the Windows Subsystem for Linux as well. I have little experience with it but that’s another cool way to do Linux things from within Windows.
    My ideal goal when it comes to computer usage is that I’ll eventually buy a brand new computer capable of running the most intense games in my Steam library that won’t run on this computer but also use these tools I mentioned to compile open source games for the fun of it.
    There is something very satisfying about being someone who plays games but also understands a lot of computer programming and how they are built from source code. Back in my Ubuntu Linux days there was a game I played and even changed the source code of to play with it.

    https://www.supertux.org/

    I remember the time I changed the gravity floating point variable and was floating above the screen. I love playing games and I love messing with computer software and testing programming languages. There is a lot to live for and I’m so glad to be free from pain and will do my best to enjoy my life.

    I remember the time I changed the gravity floating point variable and was floating above the screen. I love playing games and I love messing with computer software and testing programming languages. There is a lot to live for and I’m so glad to be free from pain and will do my best to enjoy my life.