From d1bbee41e1b9273b2c898dbda2830162b9c4d471 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Tue, 12 Sep 2023 20:54:58 +0200 Subject: [PATCH] basic setup with cairo --- CMakeLists.txt | 11 +++++++++++ src/main.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9e1c325 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.18) +project(chess C) +file(GLOB SOURCES "src/*.c") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtk4) + +add_executable(${PROJECT_NAME} ${SOURCES}) +set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99) +target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::GTK4) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c33ee87 --- /dev/null +++ b/src/main.c @@ -0,0 +1,40 @@ +#include +#include +#include + +static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer data) { + const double fieldSize = (double)(width < height ? width : height) / 8; + const double xOffset = (width - fieldSize * 8) / 2; + const double yOffset = (height - fieldSize * 8) / 2; + for(int x = 0; x < 8; ++x) { + for(int y = 0; y < 8; ++y) { + cairo_rectangle(cr, xOffset + x * fieldSize, yOffset + y * fieldSize, fieldSize, fieldSize); + if((x + y) & 1) cairo_set_source_rgb(cr, 0.46274, 0.5882, 0.3373); + else cairo_set_source_rgb(cr, 0.9333, 0.9333, 0.8235); + cairo_fill(cr); + } + } +} + +static void app_activate(GApplication *app, gpointer user_data) { + GtkWindow *win; + GtkWidget *drawArea; + + win = GTK_WINDOW(gtk_window_new()); + drawArea = gtk_drawing_area_new(); + gtk_window_set_child(win, drawArea); + gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(drawArea), draw_event, NULL, NULL); + gtk_window_set_application(win, GTK_APPLICATION(app)); + gtk_window_present(win); +} + +int main(int argc, char **argv) { + GtkApplication *app; + int stat; + + app = gtk_application_new("org.zinkel.chess", G_APPLICATION_DEFAULT_FLAGS); + g_signal_connect(app, "activate", G_CALLBACK (app_activate), NULL); + stat = g_application_run(G_APPLICATION(app), argc, argv); + g_object_unref (app); + return stat; +}