Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Makefile
CMakeCache.txt
cmake_install.cmake
install_manifest.txt
CMakeFiles/
libFGL.so*
FGL*.dll
FGL*.lib
libFGL.a*
0*
examples/01_basic_sysinfo
examples/01_basic_sysinfo.c
examples/01_basic_window
examples/01_basic_window.exe
examples/02_basic_keyboard
examples/02_basic_keyboard.exe
examples/03_basic_mouse
examples/03_basic_mouse.exe
examples/04_basic_shapes
examples/04_basic_shapes.exe
examples/05_basic_sprites
examples/05_basic_sprites.exe
examples/06_basic_text
examples/06_basic_text.exe
src/*.o
src/*.so
src/*.lo
src/*.a
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "include/external/stb"]
path = include/external/stb
url = https://github.com/nothings/stb
[submodule "include/external/fontstash"]
path = include/external/fontstash
url = https://github.com/memononen/fontstash/
114 changes: 114 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
cmake_minimum_required( VERSION 3.10 )

project( FGL VERSION 0.1 LANGUAGES C )
set(PROJECT_DESCRIPTION "Funky Graphics Library is an OpenGL platform framework written in C")

set( CMAKE_C_STANDARD 23 )
set( CMAKE_C_STANDARD_REQUIRED ON )
set( CMAKE_C_EXTENSIONS ON )

include(GNUInstallDirs)

option(BUILD_SHARED_LIBS "Build shared libraries." ON)
option(BUILD_STATIC_LIBS "Build static libraries." ON)
option(BUILD_EXAMPLES "Build examples/samples." ON)
option(INSTALL_EXAMPLE_SOURCES "Install example/sample sources." ON)
option(GARBAGE_COLLECT "Link time garbage collect sections using -Wl,--gc-sections." ON)
option(STRIP_BINARIES "Strip unused sections of generated binaries using -Wl,-s." ON)

# provide Release and Debug build types
if( NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE )
endif()
if( CMAKE_BUILD_TYPE STREQUAL Debug )
set(STRIP_BINARIES OFF)
# enable debug symbols
add_compile_options(-g)
# enable debug output for garbage collection
add_link_options(-Wl,--print-gc-sections)
endif()
# use native architecture and set optimization level
if( NOT OPTIMIZATION_LEVEL )
set( OPTIMIZATION_LEVEL 3 CACHE STRING "Compile optimization level." FORCE )
endif()
add_compile_options(-march=native -O${OPTIMIZATION_LEVEL})
# use SSE SIMD math for x86_64
if( CMAKE_GENERATOR_PLATFORM STREQUAL "x64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "x86_64" OR CMAKE_GENERATOR_PLATFORM STREQUAL "AMD64" )
add_compile_options(-mfpmath=sse)
endif()
if(STRIP_BINARIES)
add_link_options(-Wl,-s)
endif()
if(GARBAGE_COLLECT)
add_link_options(-Wl,--gc-sections)
endif()
add_compile_options(-fstdarg-opt)
add_compile_options(-fdata-sections)
add_compile_options(-fpermissive)
add_compile_options(-ffunction-sections)
add_compile_options(-D_FILE_OFFSET_BITS=64)
add_compile_options(-Wno-array-bounds)
add_compile_options(-Wno-unused-parameter)
add_compile_options(-Wno-unused-result)
add_compile_options(-Wno-pedantic)
add_compile_options(-Wno-narrowing)

# use all the C files in src/ directory
FILE(GLOB FGL_SOURCES src/*.c)
# set include directory path to include/ directory
include_directories(INTERFACE
PUBLIC_HEADER $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
PRIVATE_HEADER $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/external/fontstash/src>
PRIVATE_HEADER $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/external/stb>
)

# check for existence of glfw3 platform framework
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
link_libraries(glfw3 m OpenGL GLU)

# build shared version of the library
if(BUILD_SHARED_LIBS)
add_library( ${PROJECT_NAME}_shared SHARED ${FGL_SOURCES})
set_target_properties(${PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME}_shared)
endif()

# build static version if examples are enabled
if(BUILD_STATIC_LIBS)
add_library( ${PROJECT_NAME}_static STATIC ${FGL_SOURCES})
set_target_properties(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME}_static)
endif()

install(FILES ${PROJECT_SOURCE_DIR}/include/fgl.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT headers)
install(FILES ${PROJECT_SOURCE_DIR}/README.md ${PROJECT_SOURCE_DIR}/LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR} COMPONENT docs)

add_library(${PROJECT_NAME} INTERFACE)
if(BUILD_SHARED_LIBS)
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}_shared)
endif()
if(BUILD_STATIC_LIBS)
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}_static)
endif()

if(BUILD_EXAMPLES AND (BUILD_SHARED_LIBS OR BUILD_STATIC_LIBS))
# use all the C files in examples/ directory and link to static version of library
FILE(GLOB FGL_EXAMPLE_SOURCES examples/*.c)
foreach(example IN ITEMS ${FGL_EXAMPLE_SOURCES})
get_filename_component(FGL_EXAMPLE_NAME ${example} NAME_WLE)
add_executable(${FGL_EXAMPLE_NAME} ${example})
set_target_properties(${FGL_EXAMPLE_NAME} PROPERTIES OUTPUT_NAME examples/${FGL_EXAMPLE_NAME})
target_link_libraries(${FGL_EXAMPLE_NAME} PUBLIC ${PROJECT_NAME})
install(TARGETS ${FGL_EXAMPLE_NAME} DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/examples")
if(INSTALL_EXAMPLE_SOURCES)
install(FILES ${example} DESTINATION "${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}/examples")
endif()
endforeach()

# copy example binaries to binary directory for out of source tree build
FILE (COPY examples/res DESTINATION "${PROJECT_BINARY_DIR}/examples")
# install examples system-wide
install(DIRECTORY examples/res DESTINATION "share/${PROJECT_NAME}/examples")
endif()

93 changes: 62 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,79 @@
![](./resources/logo.png)

# Funky Graphics Library
a library inspired by raylib to get working with OpenGL easily

an orthographic [OpenGL][3] 2D/3D graphics platform framework [C23][1] library following the [KISS][5] design principle using [glfw][4]
## features
- easy and simple to understand
- create windows easily
- keyboard and mouse input
- shapes drawing such as pixels, triangles, lines and rectangles
- orthographic 2D [OpenGL][3] graphics
- intervall timer based fps performance counter
- [glfontstash][6] and [stbtruetype][7] based font text rendering
- simple shapes drawing such as pixels, triangles, lines and rectangles
- easy sprite rendering and loading
- easy text drawing and font loading

- simplified HID device input based on [glfw][4]
## dependencies
- [C23][1]
- [CMake][2]
- [OpenGL][3]
- [glfw][4]
## building and installing library
for now it's only available as a shared library and for Linux. I'll be working on cross-platform compatibility.
```sh
git clone --recurse-submodules --recursive https://github.com/mrfunkdude/fgl.git
cd fgl
cmake . --install-prefix=/usr
make && sudo make install
```
git clone https://github.com/mrfunkdude/fgl.git
cd fgl/src
chmod 700 install.sh
make && sudo ./install
```

## example application
this example will create a blank window with a text that says "hello fgl!"
```
this example will create a resizable window with a 2D HUD system information display.
Pressing 'V' enables/disables vsync, causing effect artifacts of the rotating blue
quad time string clock hand.

```c
#include "fgl.h"

fgl_vec2i size = {1024, 600};
fgl_font fnt;

int main()
{
fgl_open_window(1024, 600, "hello fgl");
fgl_font fnt = fgl_load_font("res/font.ttf");

while (!fgl_is_window_closed()) {
fgl_start_drawing();
fgl_set_background(FGL_WHITE);
fgl_draw_text(fnt, "hello fgl!", 10, 10, 30, FGL_DARKGRAY);
fgl_stop_drawing();
}

fgl_close_window();
return 0;
const char* sysinfo = fgl_sysinfo_string();
fgl_open_window(size.x, size.y, "fgl example 01 - basic sysinfo");
fnt = fgl_load_font("res/font.ttf");

while (fgl_update_window()) {
if (fgl_is_window_resized()) size = fgl_get_window_size();
if (fgl_is_key_pressed(KEY_V)) fgl_window_flip_vsync();

fgl_start_drawing();
fgl_set_background(FGL_BLACK);

// rotating blue quad with time string as clock hand
glPushMatrix();
glTranslatef((float)(size.x>>1),(float)(size.y>>1),0.0f);
glRotatef(fgl_window_frames(),0.0f,0.0f,1.0f);
fgl_draw_rectangle(-20,-20,40,40,FGL_BLUE);
fgl_draw_text(fnt, fgl_time_string(), 0, -4, 13, FGL_WHITE);
glPopMatrix();

// ortho 2d text hud
glPushMatrix();
fgl_draw_text(fnt, fgl_time_string(), 0, 3, 13, FGL_WHITE);
fgl_draw_text(fnt, fgl_window_fps_string(), size.x - 60, 3, 13, FGL_WHITE);
fgl_draw_text(fnt, sysinfo, 0, size.y - 13, 13, FGL_WHITE);
glPopMatrix();
fgl_stop_drawing();
}

fgl_close_window();
exit(EXIT_SUCCESS);
}
```
once installed the library, run the following command
```
```sh
gcc your_source.c -lFGL -o your_binary
```
![](./resources/demo.png)
[1]: https://www.open-std.org/JTC1/SC22/WG14/
[2]: https://cmake.org/
[3]: https://www.opengl.org/
[4]: https://www.glfw.org/
[5]: https://en.wikipedia.org/wiki/KISS_principle/
[6]: https://github.com/memononen/fontstash
[7]: https://github.com/nothings/stb
17 changes: 0 additions & 17 deletions examples/01_basic_window.c

This file was deleted.

75 changes: 40 additions & 35 deletions examples/02_basic_keyboard.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
#include "fgl.h"

fgl_vec2i size = {1024, 600};

int main()
{
fgl_open_window(1024, 600, "fgl example 02 - basic keyboard input");
fgl_font fnt = fgl_load_font("res/font.ttf");
fgl_rec rec = (fgl_rec){ 1024/2, 600/2, 64, 64 };
int mode = 0;

while (!fgl_is_window_closed()) {
// updating square movement
if (fgl_is_key_down(KEY_W))
rec.y -= 4;
if (fgl_is_key_down(KEY_A))
rec.x -= 4;
if (fgl_is_key_down(KEY_S))
rec.y += 4;
if (fgl_is_key_down(KEY_D))
rec.x += 4;

if (fgl_is_key_pressed(KEY_SPACE)) {
mode++;

if (mode > 1) {
mode = 0;
}
}

fgl_start_drawing();
fgl_set_background(FGL_WHITE);
mode == 0 ? fgl_draw_rectangle(rec.x, rec.y, rec.w, rec.h, FGL_RED) : fgl_draw_rectangle_outline(rec.x, rec.y, rec.w, rec.h, FGL_RED);
fgl_draw_text(fnt, "press the wasd keys to move the block around", 10, 10, 30, FGL_DARKGRAY);
fgl_draw_text(fnt, "press space to see the outline of the block", 10, 50, 30, FGL_DARKGRAY);
fgl_stop_drawing();
}

fgl_close_window();
return 0;
}
fgl_open_window(size.x, size.y, "fgl example 02 - basic keyboard input");
fgl_font fnt = fgl_load_font("res/font.ttf");
fgl_rec rec = (fgl_rec){ 1024/2, 600/2, 64, 64 };
int mode = 0;

while (fgl_update_window()) {
if (fgl_is_window_resized()) size = fgl_get_window_size();
if (fgl_is_key_pressed(KEY_V)) fgl_window_flip_vsync();

// updating square movement
if (fgl_is_key_down(KEY_W))
rec.y -= 4;
if (fgl_is_key_down(KEY_A))
rec.x -= 4;
if (fgl_is_key_down(KEY_S))
rec.y += 4;
if (fgl_is_key_down(KEY_D))
rec.x += 4;

if (fgl_is_key_pressed(KEY_SPACE)) {
mode++;

if (mode > 1) {
mode = 0;
}
}

fgl_start_drawing();
fgl_set_background(FGL_WHITE);
mode == 0 ? fgl_draw_rectangle(rec.x, rec.y, rec.w, rec.h, FGL_RED) : fgl_draw_rectangle_outline(rec.x, rec.y, rec.w, rec.h, FGL_RED);
fgl_draw_text(fnt, "press the wasd keys to move the block around", 10, 10, 30, FGL_DARKGRAY);
fgl_draw_text(fnt, "press space to see the outline of the block", 10, 50, 30, FGL_DARKGRAY);
fgl_stop_drawing();
}

fgl_close_window();
exit(EXIT_SUCCESS);
}
Loading