Add changes to control the bmp image with key events, added chatgpt to links ;)
This commit is contained in:
		
							parent
							
								
									834dad44ee
								
							
						
					
					
						commit
						b40253c895
					
				
							
								
								
									
										4
									
								
								.fflinks
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								.fflinks
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					https://lazyfoo.net/tutorials/SDL/index.php
 | 
				
			||||||
 | 
					https://ziglang.org/documentation/0.13.0/std/
 | 
				
			||||||
 | 
					https://ziglang.org/documentation/0.13.0/
 | 
				
			||||||
 | 
					https://chatgpt.com/
 | 
				
			||||||
							
								
								
									
										116
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								src/main.zig
									
									
									
									
									
								
							@ -4,45 +4,25 @@ const c = @cImport({
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const std = @import("std");
 | 
					const std = @import("std");
 | 
				
			||||||
const assert = std.debug.assert;
 | 
					const assert = std.debug.assert;
 | 
				
			||||||
 | 
					const SCREEN_WIDTH = 640;
 | 
				
			||||||
 | 
					const SCREEN_HEIGHT = 480;
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					var img_pos: Offset = .{ .x = 0, .y = 0 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn main() !void {
 | 
					pub fn main() !void {
 | 
				
			||||||
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
 | 
					    try init();
 | 
				
			||||||
        c.SDL_Log("Unable to initialize SDL: %s", c.SDL_GetError());
 | 
					    defer close();
 | 
				
			||||||
        return error.SDLInitializationFailed;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    defer c.SDL_Quit();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const screen = c.SDL_CreateWindow("My Game Window", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, 400, 140, c.SDL_WINDOW_OPENGL) orelse
 | 
					    try loadMedia();
 | 
				
			||||||
        {
 | 
					    var src_rect: c.struct_SDL_Rect = c.SDL_Rect{ .x = 10, .y = 10, .w = 400, .h = 140 };
 | 
				
			||||||
        c.SDL_Log("Unable to create window: %s", c.SDL_GetError());
 | 
					 | 
				
			||||||
        return error.SDLInitializationFailed;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    defer c.SDL_DestroyWindow(screen);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const renderer = c.SDL_CreateRenderer(screen, -1, 0) orelse {
 | 
					 | 
				
			||||||
        c.SDL_Log("Unable to create renderer: %s", c.SDL_GetError());
 | 
					 | 
				
			||||||
        return error.SDLInitializationFailed;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    defer c.SDL_DestroyRenderer(renderer);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const zig_bmp = @embedFile("zig.bmp");
 | 
					 | 
				
			||||||
    const rw = c.SDL_RWFromConstMem(zig_bmp, zig_bmp.len) orelse {
 | 
					 | 
				
			||||||
        c.SDL_Log("Unable to get RWFromConstMem: %s", c.SDL_GetError());
 | 
					 | 
				
			||||||
        return error.SDLInitializationFailed;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    defer assert(c.SDL_RWclose(rw) == 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const zig_surface = c.SDL_LoadBMP_RW(rw, 0) orelse {
 | 
					 | 
				
			||||||
        c.SDL_Log("Unable to load bmp: %s", c.SDL_GetError());
 | 
					 | 
				
			||||||
        return error.SDLInitializationFailed;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    defer c.SDL_FreeSurface(zig_surface);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const zig_texture = c.SDL_CreateTextureFromSurface(renderer, zig_surface) orelse {
 | 
					 | 
				
			||||||
        c.SDL_Log("Unable to create texture from surface: %s", c.SDL_GetError());
 | 
					 | 
				
			||||||
        return error.SDLInitializationFailed;
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
    defer c.SDL_DestroyTexture(zig_texture);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var quit = false;
 | 
					    var quit = false;
 | 
				
			||||||
    while (!quit) {
 | 
					    while (!quit) {
 | 
				
			||||||
@ -52,21 +32,67 @@ pub fn main() !void {
 | 
				
			|||||||
                c.SDL_QUIT => {
 | 
					                c.SDL_QUIT => {
 | 
				
			||||||
                    quit = true;
 | 
					                    quit = true;
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
 | 
					                c.SDL_KEYDOWN => {
 | 
				
			||||||
 | 
					                    switch (event.key.keysym.sym) {
 | 
				
			||||||
 | 
					                        c.SDLK_UP => {
 | 
				
			||||||
 | 
					                            img_pos.y = img_pos.y - 5;
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        c.SDLK_DOWN => {
 | 
				
			||||||
 | 
					                            img_pos.y = img_pos.y + 5;
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        c.SDLK_LEFT => {
 | 
				
			||||||
 | 
					                            img_pos.x = img_pos.x + 5;
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        c.SDLK_RIGHT => {
 | 
				
			||||||
 | 
					                            img_pos.x = img_pos.x - 5;
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        else => {},
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    log("Got key event: %i\n", event.key.keysym.sym);
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
                else => {},
 | 
					                else => {},
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        _ = 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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _ = c.SDL_RenderClear(renderer);
 | 
					        _ = c.SDL_UpdateWindowSurface(window);
 | 
				
			||||||
        _ = c.SDL_RenderCopy(renderer, zig_texture, null, null);
 | 
					 | 
				
			||||||
        c.SDL_RenderPresent(renderer);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        c.SDL_Delay(17);
 | 
					        c.SDL_Delay(17);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    return;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn init() !void {
 | 
				
			||||||
 | 
					    // Init SDL with video subsystem flag
 | 
				
			||||||
 | 
					    if (c.SDL_Init(c.SDL_INIT_VIDEO) < 0) {
 | 
				
			||||||
 | 
					        log("Unable to initialize SDL: %s\n", c.SDL_GetError());
 | 
				
			||||||
 | 
					        return error.SDLInitializationFailed;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const opt_window = c.SDL_CreateWindow("Game Window", c.SDL_WINDOWPOS_UNDEFINED, c.SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, c.SDL_WINDOW_SHOWN);
 | 
				
			||||||
 | 
					    if (opt_window == null) {
 | 
				
			||||||
 | 
					        log("Unable to initialize SDL window: %s\n", c.SDL_GetError());
 | 
				
			||||||
 | 
					        return error.SDLInitializationFailed;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test "simple test" {
 | 
					fn close() void {
 | 
				
			||||||
    var list = std.ArrayList(i32).init(std.testing.allocator);
 | 
					    c.SDL_FreeSurface(zig_image);
 | 
				
			||||||
    defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
 | 
					    c.SDL_DestroyWindow(window);
 | 
				
			||||||
    try list.append(42);
 | 
					    c.SDL_Quit();
 | 
				
			||||||
    try std.testing.expectEqual(@as(i32, 42), list.pop());
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user