rts

rts.git
git clone git://git.lenczewski.org/rts.git
Log | Files | Refs

linux.h (3050B)


      1 #ifndef PLATFORM_LINUX_H
      2 #define PLATFORM_LINUX_H
      3 
      4 #define _XOPEN_SOURCE 700
      5 #define _GNU_SOURCE 1
      6 #define _DEFAULT_SOURCE 1
      7 
      8 #include <limits.h>
      9 #include <stdio.h>
     10 #include <string.h>
     11 #include <unistd.h>
     12 
     13 #include <sys/mman.h>
     14 
     15 #include <dlfcn.h>
     16 
     17 #include <sys/stat.h>
     18 #include <fcntl.h>
     19 
     20 #include <alsa/asoundlib.h>
     21 
     22 #include <xkbcommon/xkbcommon.h>
     23 #include <linux/input-event-codes.h>
     24 
     25 #include <wayland-client.h>
     26 #include "xdg-shell.h"
     27 
     28 #include <wayland-egl.h>
     29 #include <EGL/egl.h>
     30 
     31 #include "../rts/api.h"
     32 #include "renderer.h"
     33 
     34 enum debug_level {
     35 	ERROR,
     36 	WARN,
     37 	INFO,
     38 	DEBUG,
     39 };
     40 
     41 extern enum debug_level loglevel;
     42 
     43 static inline void
     44 dbglog(enum debug_level level, char const *fmt, ...)
     45 {
     46 	if (level > loglevel)
     47 		return;
     48 
     49 	va_list ap;
     50 	va_start(ap, fmt);
     51 	vfprintf(stderr, fmt, ap);
     52 	va_end(ap);
     53 }
     54 
     55 #define PLATFORM_MEMORY (64 * MiB)
     56 #define GAME_MEMORY (64 * MiB)
     57 
     58 struct linux_thread {
     59 	u32 id;
     60 };
     61 
     62 #if 0
     63 #define RTS_AUDIO_CHANNELS 2
     64 #define RTS_AUDIO_SAMPLE_RATE 48000
     65 #define RTS_AUDIO_SAMPLE_BITS 32
     66 
     67 #define RTS_AUDIO_FRAME_BYTES \
     68 	(RTS_AUDIO_CHANNELS * (RTS_AUDIO_SAMPLE_BITS / 8))
     69 
     70 struct audio {
     71 	void *buffer;
     72 	u32 channels;
     73 	u32 sample_rate;
     74 	u32 sample_bits;
     75 	u32 expected_frames;
     76 };
     77 #endif
     78 
     79 struct linux_audio {
     80 	snd_pcm_t *handle;
     81 
     82 	u32 period_us;
     83 	snd_pcm_uframes_t period_frames;
     84 	snd_pcm_uframes_t buffer_frames;
     85 
     86 	struct audio audio_buffer;
     87 
     88 	b32 enabled;
     89 };
     90 
     91 struct linux_video {
     92 	struct wl_display *display;
     93 	struct wl_registry *registry;
     94 
     95 	struct wl_compositor *compositor;
     96 	struct wl_seat *seat;
     97 	struct xdg_wm_base *wm_base;
     98 
     99 	struct wl_pointer *pointer;
    100 	struct wl_keyboard *keyboard;
    101 
    102 	struct xkb_context *xkb_context;
    103 	struct xkb_keymap *xkb_keymap;
    104 	struct xkb_state *xkb_state;
    105 
    106 	struct input input;
    107 
    108 	struct wl_surface *surface;
    109 	struct xdg_surface *xdg_surface;
    110 	struct xdg_toplevel *xdg_toplevel;
    111 
    112 	struct wl_egl_window *egl_window;
    113 	EGLDisplay egl_display;
    114 	EGLContext egl_context;
    115 	EGLSurface egl_surface;
    116 
    117 	struct renderer *renderer;
    118 
    119 	u32 width, height;
    120 };
    121 
    122 struct linux_state {
    123 	struct memory memory;
    124 
    125 	struct linux_audio audio;
    126 	struct linux_video video;
    127 
    128 	struct platform_api platform_api;
    129 	struct renderer_api renderer_api;
    130 	struct rts_api rts_api;
    131 
    132 	b32 running;
    133 };
    134 
    135 internal u64
    136 linux_elapsed_ns(struct timespec *restrict start, struct timespec *restrict end);
    137 
    138 internal b32
    139 load_game_api(char const *path, struct rts_api *api);
    140 
    141 internal void
    142 load_platform_api(struct platform_api *api);
    143 
    144 internal b32
    145 init_memory(struct memory *memory, u64 capacity);
    146 
    147 internal void
    148 handle_pending_events(struct linux_state *state);
    149 
    150 internal b32
    151 init_audio(struct arena *arena, struct linux_audio *audio,
    152 	   u32 period_samples, u32 buffer_samples);
    153 
    154 internal void
    155 reset_audio(struct linux_audio *audio);
    156 
    157 internal void
    158 update_expected_frames(struct linux_audio *audio, f32 dt);
    159 
    160 internal void
    161 play_audio(struct linux_audio *audio);
    162 
    163 internal b32
    164 init_video(struct arena *arena, struct linux_video *video, char const *title);
    165 
    166 internal void
    167 draw_frame(struct linux_video *video);
    168 
    169 #endif /* PLATFORM_LINUX_H */