diff options
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | cat.png | bin | 0 -> 34282 bytes | |||
| -rw-r--r-- | cmd/demo/main.ha | 24 | ||||
| -rw-r--r-- | sdl2/image/image.ha | 28 | ||||
| -rw-r--r-- | sdl2/pixels.ha | 38 | ||||
| -rw-r--r-- | sdl2/rect.ha | 29 | ||||
| -rw-r--r-- | sdl2/render.ha | 16 | ||||
| -rw-r--r-- | sdl2/surface.ha | 25 |
8 files changed, 163 insertions, 3 deletions
@@ -1,10 +1,12 @@ .POSIX: .SUFFIXES: +LIBS=-lc -lSDL2_image -lSDL2 + demo: - hare build -lSDL2 -lc -T+libc cmd/demo + hare build $(LIBS) -T+libc cmd/demo run: - hare run -lSDL2 -lc -T+libc cmd/demo + hare run $(LIBS) -T+libc cmd/demo .PHONY: demo run Binary files differdiff --git a/cmd/demo/main.ha b/cmd/demo/main.ha index 26025ce..35da638 100644 --- a/cmd/demo/main.ha +++ b/cmd/demo/main.ha @@ -2,11 +2,14 @@ use fmt; use os; use sdl2::{event_type, window_flags, renderer_flags}; use sdl2; +use sdl2::image; use strings; export fn main() void = { sdl2::init(sdl2::init_flags::VIDEO); defer sdl2::quit(); + image::init(image::init_flags::PNG); + defer image::quit(); const title = strings::to_c("Hare SDL2 demo"); defer free(title); @@ -28,8 +31,27 @@ export fn main() void = { }; defer sdl2::destroy_renderer(render); - sdl2::set_render_draw_color(render, 255, 0, 0, 255); + const path = strings::to_c("cat.png"); + defer free(path); + const texture = match (image::load_texture(render, path)) { + case let tex: *sdl2::texture => + yield tex; + case null => + fmt::fatal("sdl2::image::load_texture failed"); + }; + defer sdl2::destroy_texture(texture); + + let width = 0, height = 0; + sdl2::query_texture(texture, null, null, &width, &height); + + sdl2::set_render_draw_color(render, 50, 50, 50, 255); sdl2::render_clear(render); + sdl2::render_copy(render, texture, null, &sdl2::rect { + x = 0, + y = 0, + w = width, + h = height, + }); sdl2::render_present(render); let ev = sdl2::event { ... }; diff --git a/sdl2/image/image.ha b/sdl2/image/image.ha new file mode 100644 index 0000000..03301e9 --- /dev/null +++ b/sdl2/image/image.ha @@ -0,0 +1,28 @@ +// TODO: Flesh me out +// TODO: SDL_RWops +use sdl2; + +// Flags for [[init]]. +export type init_flags = enum int { + NONE = 0, + JPG = 0x00000001, + PNG = 0x00000002, + TIF = 0x00000004, + WEBP = 0x00000008, +}; + +// Loads dynamic libraries and prepares them for use. Flags should be one or +// more flags from IMG_InitFlags OR'd together. +// +// Returns the flags successfully initialized, or 0 on failure. +export @symbol("IMG_Init") fn init(flags: init_flags) int; + +// Unloads libraries loaded with [[init]] +export @symbol("IMG_Quit") fn quit() void; + +// Load an image from a file path. +export @symbol("IMG_Load") fn load(file: const *char) nullable *sdl2::surface; + +// Load an image directly into a render texture. +export @symbol("IMG_LoadTexture") fn load_texture(renderer: *sdl2::renderer, + file: const *char) nullable *sdl2::texture; diff --git a/sdl2/pixels.ha b/sdl2/pixels.ha new file mode 100644 index 0000000..8227067 --- /dev/null +++ b/sdl2/pixels.ha @@ -0,0 +1,38 @@ +// TODO: Flesh me out + +export type color = struct { + r: u8, + g: u8, + b: u8, + a: u8, +}; + +export type palette = struct { + ncolors: int, + colors: *color, + version: u32, + refcount: int, +}; + +// Note: Everything in the pixel format structure is read-only. +export type pixelformat = struct { + format: u32, // TODO + palette: *palette, + bitsperpixel: u8, + bytesperpixel: u8, + padding: [2]u8, + rmask: u32, + gmask: u32, + bmask: u32, + amask: u32, + rloss: u8, + gloss: u8, + bloss: u8, + aloss: u8, + rshift: u8, + gshift: u8, + bshift: u8, + ashift: u8, + refcount: int, + next: nullable *pixelformat, +}; diff --git a/sdl2/rect.ha b/sdl2/rect.ha new file mode 100644 index 0000000..00fae87 --- /dev/null +++ b/sdl2/rect.ha @@ -0,0 +1,29 @@ +// TODO: Flesh me out + +// The structure that defines a point (integer) +export type point = struct { + x: int, + y: int, +}; + +// The structure that defines a point (floating point) +export type fpoint = struct { + x: f32, + y: f32, +}; + +// A rectangle, with the origin at the upper left (integer). +export type rect = struct { + x: int, + y: int, + w: int, + h: int, +}; + +// A rectangle, with the origin at the upper left (floating point). +export type frect = struct { + x: f32, + y: f32, + w: f32, + h: f32, +}; diff --git a/sdl2/render.ha b/sdl2/render.ha index e1d2c8e..d4f0671 100644 --- a/sdl2/render.ha +++ b/sdl2/render.ha @@ -68,3 +68,19 @@ export @symbol("SDL_RenderClear") fn render_clear(renderer: *renderer) int; // Update the screen with rendering performed. export @symbol("SDL_RenderPresent") fn render_present(renderer: *renderer) void; + +// Destroy the specified texture. +export @symbol("SDL_DestroyTexture") fn destroy_texture(texture: *texture) void; + +// Query the attributes of a texture +// +// Returns 0 on success, or -1 if the texture is not valid. +export @symbol("SDL_QueryTexture") fn query_texture(texture: *texture, + format: nullable *u32, access: nullable *int, + w: nullable *int, h: nullable *int) int; + +// Copy a portion of the texture to the current rendering target. +// +// Returns 0 on success, or -1 on error +export @symbol("SDL_RenderCopy") fn render_copy(renderer: *renderer, + texture: *texture, srcrect: nullable *rect, dstrect: nullable *rect) int; diff --git a/sdl2/surface.ha b/sdl2/surface.ha new file mode 100644 index 0000000..7bbe850 --- /dev/null +++ b/sdl2/surface.ha @@ -0,0 +1,25 @@ +// A collection of pixels used in software blitting. +// +// This structure should be treated as read-only, except for 'pixels', which, if +// not null, contains the raw pixel data for the surface. +export type surface = struct { + flags: u32, + format: *pixelformat, + w: int, + h: int, + pitch: int, + pixels: nullable *void, + + userdata: *void, + + locked: int, + lock_data: *void, + + clip_rect: rect, + + map: *blitmap, + + refcount: int, +}; + +export type blitmap = void; |
