Added scaling and ready to start with sdl_image

This commit is contained in:
2025-01-02 08:28:46 -07:00
parent b40253c895
commit a335d10f9f
7 changed files with 76 additions and 43 deletions
+30 -17
View File
@@ -1,5 +1,6 @@
const c = @cImport({
@cInclude("SDL2/SDL.h");
@cInclude("SDL2/SDL_image.h");
});
const std = @import("std");
@@ -10,7 +11,6 @@ const log = c.SDL_Log;
var window: *c.struct_SDL_Window = undefined;
var screen_surface: *c.struct_SDL_Surface = undefined;
var zig_image: *c.struct_SDL_Surface = undefined;
const Offset = struct {
x: i32,
y: i32,
@@ -21,8 +21,10 @@ pub fn main() !void {
try init();
defer close();
try loadMedia();
var src_rect: c.struct_SDL_Rect = c.SDL_Rect{ .x = 10, .y = 10, .w = 400, .h = 140 };
const zig_image = try loadSurface("assets/stretch.bmp");
defer c.SDL_FreeSurface(zig_image);
var src_rect: c.struct_SDL_Rect = c.SDL_Rect{ .x = 0, .y = 0, .w = SCREEN_WIDTH / 2, .h = SCREEN_HEIGHT / 2 };
var quit = false;
while (!quit) {
@@ -41,10 +43,10 @@ pub fn main() !void {
img_pos.y = img_pos.y + 5;
},
c.SDLK_LEFT => {
img_pos.x = img_pos.x + 5;
img_pos.x = img_pos.x - 5;
},
c.SDLK_RIGHT => {
img_pos.x = img_pos.x - 5;
img_pos.x = img_pos.x + 5;
},
else => {},
}
@@ -54,8 +56,8 @@ pub fn main() !void {
}
}
_ = c.SDL_FillRect(screen_surface, null, c.SDL_MapRGB(screen_surface.format, 0xff, 0xff, 0xff));
var dest_rect = c.SDL_Rect{ .x = src_rect.x + img_pos.x, .y = src_rect.y + img_pos.y, .w = src_rect.w, .h = src_rect.h };
_ = c.SDL_BlitSurface(zig_image, &src_rect, screen_surface, &dest_rect);
var dest_rect = c.SDL_Rect{ .x = src_rect.x + img_pos.x, .y = src_rect.y + img_pos.y, .w = SCREEN_WIDTH, .h = SCREEN_HEIGHT };
_ = c.SDL_BlitScaled(zig_image, &src_rect, screen_surface, &dest_rect);
_ = c.SDL_UpdateWindowSurface(window);
c.SDL_Delay(17);
@@ -77,22 +79,33 @@ fn init() !void {
}
window = opt_window.?;
// get surface and fill with white
screen_surface = c.SDL_GetWindowSurface(window);
}
fn loadMedia() !void {
const image: [*c]c.struct_SDL_Surface = c.SDL_LoadBMP("src/zig.bmp");
if (image == null) {
log("Unable to load media: %s\n", c.SDL_GetError());
return error.SDLLoadError;
} else {
zig_image = image;
const img_flags = c.IMG_INIT_PNG;
if (c.IMG_Init(img_flags) != img_flags) {
log("Unable to initialize SDL Image: %s\n", c.IMG_GetError());
return error.SDLInitializationFailed;
}
}
fn loadSurface(path: [*c]const u8) !*c.struct_SDL_Surface {
// TODO check file exists
const loaded_surface: [*c]c.struct_SDL_Surface = c.SDL_LoadBMP(path);
if (loaded_surface == null) {
log("Unable to load media at path %s: %s\n", path, c.SDL_GetError());
return error.SDLLoadError;
}
defer c.SDL_FreeSurface(loaded_surface);
// Converting 24bit bmp to 32bit to match display
const optimized_surface = c.SDL_ConvertSurface(loaded_surface, screen_surface.format, 0);
if (optimized_surface == null) {
log("Unable to optimize media at path %s: %s\n", path, c.SDL_GetError());
return error.SDLLoadError;
}
return optimized_surface;
}
fn close() void {
c.SDL_FreeSurface(zig_image);
c.SDL_DestroyWindow(window);
c.SDL_Quit();
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 164 KiB