Addin cmdline mode. Need to do something with string<->cstring
This commit is contained in:
		
							
								
								
									
										126
									
								
								main.odin
									
									
									
									
									
								
							
							
						
						
									
										126
									
								
								main.odin
									
									
									
									
									
								
							@ -3,6 +3,8 @@ package main
 | 
				
			|||||||
import "core:fmt"
 | 
					import "core:fmt"
 | 
				
			||||||
import rl "vendor:raylib"
 | 
					import rl "vendor:raylib"
 | 
				
			||||||
import "core:mem"
 | 
					import "core:mem"
 | 
				
			||||||
 | 
					import "core:os"
 | 
				
			||||||
 | 
					import "core:strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
W :: 400
 | 
					W :: 400
 | 
				
			||||||
H :: 300
 | 
					H :: 300
 | 
				
			||||||
@ -20,6 +22,7 @@ Cart :: struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
WrongSizeErrorTimer : i32 = 0
 | 
					WrongSizeErrorTimer : i32 = 0
 | 
				
			||||||
 | 
					IsConverted : bool = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
main :: proc() {
 | 
					main :: proc() {
 | 
				
			||||||
	when ODIN_DEBUG {
 | 
						when ODIN_DEBUG {
 | 
				
			||||||
@ -43,7 +46,70 @@ main :: proc() {
 | 
				
			|||||||
			mem.tracking_allocator_destroy(&track)
 | 
								mem.tracking_allocator_destroy(&track)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						args := os.args[1:]
 | 
				
			||||||
 | 
						if len(args) == 0 {
 | 
				
			||||||
 | 
							GraphicsMode()
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(args) != 3 {
 | 
				
			||||||
 | 
							fmt.println(
 | 
				
			||||||
 | 
								"  PicoRepico cmdline usage:",
 | 
				
			||||||
 | 
								"repico <input.p8.png> <shell.png> <output.p8.png>",
 | 
				
			||||||
 | 
								"Use only 160x205 png images with PICO-8 palette",
 | 
				
			||||||
 | 
								sep = "\n",
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
 | 
							return 
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						CmdMode(args)
 | 
				
			||||||
 | 
					} 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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!")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					GraphicsMode :: proc() {
 | 
				
			||||||
	cart := Cart{}
 | 
						cart := Cart{}
 | 
				
			||||||
	target := Cart{}
 | 
						target := Cart{}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
@ -52,8 +118,6 @@ main :: proc() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	hw : f32 = auto_cast width / 2
 | 
						hw : f32 = auto_cast width / 2
 | 
				
			||||||
	hh : f32 = auto_cast height / 2
 | 
						hh : f32 = auto_cast height / 2
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	buttonCartRect := rl.Rectangle{
 | 
						buttonCartRect := rl.Rectangle{
 | 
				
			||||||
		x = hw - CW - 20,
 | 
							x = hw - CW - 20,
 | 
				
			||||||
		y = 8,
 | 
							y = 8,
 | 
				
			||||||
@ -125,47 +189,31 @@ main :: proc() {
 | 
				
			|||||||
	unloadCart(&target)
 | 
						unloadCart(&target)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CartLoadError :: enum {
 | 
					
 | 
				
			||||||
	None,
 | 
					CmdMode :: proc(args: []string) {
 | 
				
			||||||
	Wrong_Size,
 | 
						argnames := []string {
 | 
				
			||||||
	Allocation_Error,
 | 
							"input file",
 | 
				
			||||||
 | 
							"shell file",
 | 
				
			||||||
 | 
							"output file",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						images := [3]rl.Image{}
 | 
				
			||||||
 | 
						i := 0
 | 
				
			||||||
 | 
						for arg in args {
 | 
				
			||||||
 | 
							if !strings.has_suffix(arg, ".png") {
 | 
				
			||||||
 | 
								fmt.printf("%v is not a .png file", argnames[i])
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
loadCart :: proc(target: ^Cart, filename: cstring) -> CartLoadError {
 | 
							images[i] = rl.LoadImage(arg)
 | 
				
			||||||
	unloadCart(target)
 | 
							if !rl.IsImageReady(images[i]) {
 | 
				
			||||||
	target.image = rl.LoadImage(filename)
 | 
								fmt.printf("can't load %v", argnames[i])
 | 
				
			||||||
	if target.image.width != CW || target.image.height != CH {
 | 
								return
 | 
				
			||||||
		return CartLoadError.Wrong_Size
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	target.texture = rl.LoadTextureFromImage(target.image)
 | 
							i += 1
 | 
				
			||||||
	target.loaded = true
 | 
					 | 
				
			||||||
	return CartLoadError.None
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer {
 | 
				
			||||||
unloadCart :: proc(target: ^Cart) {
 | 
							for img in images {
 | 
				
			||||||
	if target.loaded {
 | 
								rl.UnloadImage(img)
 | 
				
			||||||
		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!")
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user