This is my proof of concept for a simple gtk4 2D action game using only available widgets and hosted on github.
Issues encountered:
Keyboard input is not fluid. I am using the GtkEventControllerKey to process keyboard inputs but since it is meant for widgets the keyboard input is kind of lagging behind. And i can’t move and shoot at the same time while holding both the arrow key and the space bar for shooting. Is there a keyboard function that i can called on each frame to process the keyboard input just as it’s usually done in a game loop?
How can i apply a tint effect on a GtkPicture?
This one has nothing to do with the game but i was just testing rotation animation of a GtkPicture using gtk_fixed_set_child_transform. I noticed that the GtkPicture is rotated with its pivot being at the top left corner. Is there a way to rotate around the center of the picture or any specific point within the picture?
That’s a pretty interesting idea, I have some suggestions, although I must admit I never thought to make a game with only GTK widgets!
You can just store some booleans like: bool left_pressed, right_pressed; and then set it to true/false on key pressed/release event. Then in your game loop you would do an action based on those booleans. GTK will send you key repeat events but this method will account for that.
I think you will have to subclass the widget and use gtk_snapshot_push_color_matrix or gtk_snapshot_push_blend. Or make it into a container widget with GtkBinLayout.
You would have to first use gsk_transform_translate to translate by values equal to half the dimensions of the image, then call gsk_transform_rotate, then translate back by the negative of the dimensions/2.
Side note: you likely don’t want to use gtk_widget_add_tick_callback for your game’s physics loop, because that will tie the game loop to the refresh rate of the monitor, which could change. You’d usually want to have a fixed step rate and then for example do g_timeout_add(1000/60, ...) for roughly 60 steps/sec. For higher precision you would have to use the underlying platform (timerfd_create or similar). To do higher FPS rendering you could then use gtk_widget_add_tick_callback again, but only to interpolate the positions of the objects based on their velocity vectors.
Your tip on the keyboard issue made me remember that the solution has already been implemented in the original code. I inadvertently deleted the codes while converting the keyboard processing from allegro to gtk4.
Yes i think your solution to the tint issue would definitely work.
But since my goal is to use only available widgets without creating new ones, i will stop here. If i really need the tint effect then think i will just create another sprite with tint and do the animation with it like old times.
Am actually building a luajit ffi bindings to gtk4 and a much higher api to the gtk4. So no subclassing.
By the way have already translated the gtk4 game to the luajit version and it just works same as its C version.
Don’t forget to share the link to the video. Am curious to see how it runs on another computer as my laptop is 12 years old and could display only ONE icon in the fishbowl demo for gtk3 and near 170 icons for gtk4 after waiting more than 20minutes