From 2a91a5efd4cf52d48f985a6a3c7536f4fcbddc27 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Sat, 16 Mar 2024 16:02:18 +0300 Subject: [PATCH] init --- .gitignore | 2 + main.odin | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++ nfrc.png | Bin 0 -> 1042 bytes 3 files changed, 173 insertions(+) create mode 100644 .gitignore create mode 100644 main.odin create mode 100644 nfrc.png diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eafe6a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +repico +ols.json diff --git a/main.odin b/main.odin new file mode 100644 index 0000000..5e56239 --- /dev/null +++ b/main.odin @@ -0,0 +1,171 @@ +package main + +import "core:fmt" +import rl "vendor:raylib" +import "core:mem" + +W :: 400 +H :: 300 +CW :: 160 +CH :: 205 + +width : int = W +height : int = H +zoom : int = 1 + +Cart :: struct { + image : rl.Image, + texture : rl.Texture, + loaded : bool, +} + +WrongSizeErrorTimer : i32 = 0 + +main :: proc() { + when ODIN_DEBUG { + track : mem.Tracking_Allocator + mem.tracking_allocator_init(&track, context.allocator) + context.allocator = mem.tracking_allocator(&track) + + defer { + if len(track.allocation_map) > 0 { + fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) + for _, entry in track.allocation_map { + fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) + } + } + if len(track.bad_free_array) > 0 { + fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) + for entry in track.bad_free_array { + fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) + } + } + mem.tracking_allocator_destroy(&track) + } + } + + cart := Cart{} + target := Cart{} + + rl.InitWindow(W, H, "PicoRepico") + rl.SetTargetFPS(60) + + hw : f32 = auto_cast width / 2 + hh : f32 = auto_cast height / 2 + + + buttonCartRect := rl.Rectangle{ + x = hw - CW - 20, + y = 8, + width = CW, + height = CH, + } + buttonTargetRect := rl.Rectangle{ + x = hw + 20, + y = 8, + width = CW, + height = CH, + } + convertButtonRect := rl.Rectangle{ + x = hw - 40, + y = auto_cast height - 38, + width = 80, + height = 30, + } + + for !rl.WindowShouldClose() { + if WrongSizeErrorTimer > 0 { + WrongSizeErrorTimer -= 1 + } + + mouse := rl.GetMousePosition() + if rl.IsFileDropped() { + droppedFiles := rl.LoadDroppedFiles() + defer rl.UnloadDroppedFiles(droppedFiles) + + if droppedFiles.count == 1 { + f := droppedFiles.paths[0] + if mouse.x <= hw { + fmt.println("Got cart file: ", f) + if loadCart(&cart, f) == .Wrong_Size { + WrongSizeErrorTimer = 180 + } + } + if mouse.x > hw { + fmt.println("Got target file: ", f) + if loadCart(&target, f) == .Wrong_Size { + WrongSizeErrorTimer = 180 + } + } + } + } + + rl.BeginDrawing() + rl.GuiDummyRec(rl.Rectangle{0, 0, auto_cast width, auto_cast height}, "") + + rl.GuiButton(buttonCartRect, "Place here cart") + rl.GuiButton(buttonTargetRect, "Place here target") + + if WrongSizeErrorTimer > 0 { + rl.GuiLabel(rl.Rectangle{8, auto_cast height - 38, 100, 30}, "WRONG IMAGE SIZE") + } + + if cart.loaded { + rl.DrawTexture(cart.texture, auto_cast buttonCartRect.x, auto_cast buttonCartRect.y, rl.WHITE) + } + if target.loaded { + rl.DrawTexture(target.texture, auto_cast buttonTargetRect.x, auto_cast buttonTargetRect.y, rl.WHITE) + } + + if rl.GuiButton(convertButtonRect, "CONVERT!") { convertCart(&cart, &target) } + rl.EndDrawing() + } + + unloadCart(&cart) + unloadCart(&target) +} + +CartLoadError :: enum { + None, + Wrong_Size, + Allocation_Error, +} + +loadCart :: proc(target: ^Cart, filename: cstring) -> CartLoadError { + unloadCart(target) + target.image = rl.LoadImage(filename) + if target.image.width != CW || target.image.height != CH { + return CartLoadError.Wrong_Size + } + target.texture = rl.LoadTextureFromImage(target.image) + target.loaded = true + return CartLoadError.None +} + +unloadCart :: proc(target: ^Cart) { + if target.loaded { + rl.UnloadTexture(target.texture) + rl.MemFree(target.image.data) + } + target.loaded = false +} + +convertCart :: proc(cart: ^Cart, target: ^Cart) { + newCartImage := rl.ImageCopy(target.image) + for x : i32 = 0; x < newCartImage.width; x += 1 { + for y : i32 = 0; y < newCartImage.height; y += 1{ + targetCol := rl.GetImageColor(target.image, x, y) + sourceCol := rl.GetImageColor(cart.image, x, y) + newCol := rl.Color{ + targetCol.r & 0b11111100 | sourceCol.r & 0b00000011, + targetCol.g & 0b11111100 | sourceCol.g & 0b00000011, + targetCol.b & 0b11111100 | sourceCol.b & 0b00000011, + targetCol.a & 0b11111100 | sourceCol.a & 0b00000011, + } + rl.ImageDrawPixel(&newCartImage, x, y, newCol) + } + } + + rl.ExportImage(newCartImage, "output.p8.png") + fmt.println("DONE!") +} diff --git a/nfrc.png b/nfrc.png new file mode 100644 index 0000000000000000000000000000000000000000..9d2f21455316105ca9d6af7b746ff0da7514a7a5 GIT binary patch literal 1042 zcmeAS@N?(olHy`uVBq!ia0vp^3xN162OE&IUs}BqNHG=%xjQkeJ16s!fr0sqr;B4q z#hkad?`LYeOB{QcUuLf0A$;6+Nk_wKj)yTz+2tR}F)6M*9J_>l`@`T0A|vbN^mT>IJP{O{>iq3iFxm;3+v=f3Yx*bDyuTlIbIt$Rx^ zF25E1N%7tOv!?y`s;=JH|3B{P^R-d(wRhHLex9-XSNhrPclCQC-`V|o_h?hJ{<(9{ z+2a}VUwrzt>b~0#(*y7K#p~Ps)v)7xW_Bp;)5N06+N;aIs9EGPsdzQGa8A%*6jF6q z#Nsi9p;Jg9a1aRUzT~xedVPOg^|f94_J5N88efx_7M`#o>~!8<3+wrpr5}EYiHk1< zisV1z`Ef(?*}u>Ib35l%{q??X|J~>7E#tfYYyT(Pe!iQ|BvkrE_`}vXE5>`(Nqb6| z_Fw%iZw=JPZ_PM=mdlIxPCb=h1VYu~QiC|2bj4ps-nx$uVKh#+eOI zOwt`D9n*+En8G<>#x=G0gHL{Nsrfaq{FH1nAT9WkmZ>=`nzcW5(&pZW`7?pW(I@zy ztTAU&@y3kq;I-y2Z3_$574*%oRHOv=~2~`=`Bc)vB=2P)L;XZ`1ZW6#OytE?b1E z)4eOjk5=E}->@p{=Gyn$l>07SS$6AKSZHvlwCVLLza$oKy_gjey45gicJ$S2%Xap@ zitgQcOCs2J?Y3O$&o)~PtFo^iTd{t+-TyD2Lqac`T$PB