Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move LCD/Video functions to its own namespace: LCD * Input support * Convert mode 4 assets to GBA native format in the build script using zigimg to read bitmap file
- Loading branch information
1 parent
0fab3de
commit fa0a513
Showing
21 changed files
with
428 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
zig-cache | ||
/refs | ||
zig-cache | ||
/refs | ||
*.agi | ||
*.agp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[submodule "GBA/assetconverter/zigimg"] | ||
path = GBA/assetconverter/zigimg | ||
url = git@github.com:mlarouche/zigimg.git | ||
branch = master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const bmp = @import("zigimg/zigimg.zig").bmp; | ||
const Allocator = @import("std").mem.Allocator; | ||
const ArrayList = @import("std").ArrayList; | ||
const OctTreeQuantizer = @import("zigimg/zigimg.zig").octree_quantizer.OctTreeQuantizer; | ||
const Color = @import("zigimg/zigimg.zig").color.Color; | ||
const fs = @import("std").fs; | ||
const mem = @import("std").mem; | ||
|
||
pub const ImageConverterError = error{InvalidPixelData}; | ||
|
||
const GBAColor = packed struct { | ||
r: u5, | ||
g: u5, | ||
b: u5, | ||
}; | ||
|
||
pub const ImageSourceTarget = struct { | ||
source: []const u8, | ||
target: []const u8, | ||
}; | ||
|
||
pub const ImageConverter = struct { | ||
pub fn convertMode4Image(allocator: *Allocator, images: []ImageSourceTarget, targetPaletteFilePath: []const u8) !void { | ||
var quantizer = OctTreeQuantizer.init(allocator); | ||
defer quantizer.deinit(); | ||
|
||
const ImageConvertInfo = struct { | ||
imageInfo: *const ImageSourceTarget, | ||
image: bmp.Bitmap, | ||
}; | ||
|
||
var imageConvertList = ArrayList(ImageConvertInfo).init(allocator); | ||
defer imageConvertList.deinit(); | ||
|
||
for (images) |imageInfo| { | ||
var convertInfo = try imageConvertList.addOne(); | ||
convertInfo.imageInfo = &imageInfo; | ||
convertInfo.image = try bmp.Bitmap.fromFile(allocator, imageInfo.source); | ||
|
||
if (convertInfo.image.pixels) |pixelData| { | ||
for (pixelData) |pixel| { | ||
try quantizer.addColor(pixel.premultipliedAlpha()); | ||
} | ||
} else { | ||
return ImageConverterError.InvalidPixelData; | ||
} | ||
} | ||
var paletteStorage: [256]Color = undefined; | ||
var palette = try quantizer.makePalette(255, paletteStorage[0..]); | ||
|
||
var paletteFile = try openWriteFile(targetPaletteFilePath); | ||
defer paletteFile.close(); | ||
|
||
var paletteOut = paletteFile.outStream(); | ||
var paletteOutStream = &paletteOut.stream; | ||
|
||
// Write palette file | ||
var paletteCount: usize = 0; | ||
for (palette) |entry| { | ||
const gbaColor = colorToGBAColor(entry); | ||
try paletteOutStream.writeIntLittle(u15, @bitCast(u15, gbaColor)); | ||
paletteCount += 2; | ||
} | ||
|
||
// Align palette file to a power of 4 | ||
var diff = mem.alignForward(paletteCount, 4) - paletteCount; | ||
var index: usize = 0; | ||
while (index < diff) : (index += 1) { | ||
try paletteOutStream.writeIntLittle(u8, 0); | ||
} | ||
|
||
for (imageConvertList.toSlice()) |convertInfo| { | ||
if (convertInfo.image.pixels) |pixelData| { | ||
var imageFile = try openWriteFile(convertInfo.imageInfo.target); | ||
defer imageFile.close(); | ||
|
||
var imageOut = imageFile.outStream(); | ||
var imageOutStream = &imageOut.stream; | ||
|
||
// Write image file | ||
var pixelCount: usize = 0; | ||
for (pixelData) |pixel| { | ||
var rawPaletteIndex: usize = try quantizer.getPaletteIndex(pixel.premultipliedAlpha()); | ||
var paletteIndex: u8 = @intCast(u8, rawPaletteIndex); | ||
try imageOutStream.writeIntLittle(u8, paletteIndex); | ||
pixelCount += 1; | ||
} | ||
|
||
diff = mem.alignForward(pixelCount, 4) - pixelCount; | ||
index = 0; | ||
while (index < diff) : (index += 1) { | ||
try imageOutStream.writeIntLittle(u8, 0); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn openWriteFile(path: []const u8) !fs.File { | ||
return try fs.cwd().createFile(path, fs.File.CreateFlags{}); | ||
} | ||
|
||
fn colorToGBAColor(color: Color) GBAColor { | ||
return GBAColor{ | ||
.r = @intCast(u5, (color.R >> 3) & 0x1f), | ||
.g = @intCast(u5, (color.G >> 3) & 0x1f), | ||
.b = @intCast(u5, (color.B >> 3) & 0x1f), | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
pub const Color = @import("color.zig").Color; | ||
pub const Debug = @import("debug.zig").Debug; | ||
pub const GBA = @import("core.zig").GBA; | ||
pub const Mode3 = @import("mode3.zig").Mode3; | ||
pub const Input = @import("input.zig").Input; | ||
pub const LCD = @import("lcd.zig").LCD; | ||
pub const Mode3 = @import("mode3.zig").Mode3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const GBA = @import("core.zig").GBA; | ||
|
||
pub const Input = struct { | ||
var previousInput: u16 = 0; | ||
var currentInput: u16 = 0; | ||
|
||
pub const Keys = struct { | ||
pub const A = 1 << 0; | ||
pub const B = 1 << 1; | ||
pub const Select = 1 << 2; | ||
pub const Start = 1 << 3; | ||
pub const Right = 1 << 4; | ||
pub const Left = 1 << 5; | ||
pub const Up = 1 << 6; | ||
pub const Down = 1 << 7; | ||
pub const R = 1 << 8; | ||
pub const L = 1 << 9; | ||
}; | ||
|
||
pub fn readInput() void { | ||
previousInput = currentInput; | ||
currentInput = ~GBA.KEYINPUT.*; | ||
} | ||
|
||
pub inline fn isKeyDown(keys: u16) bool { | ||
return (currentInput & keys) == keys; | ||
} | ||
|
||
pub inline fn isKeyJustPressed(keys: u16) bool { | ||
return (previousInput & keys) == 0 and (currentInput & keys) == keys; | ||
} | ||
|
||
pub inline fn isKeyJustReleased(keys: u16) bool { | ||
return (previousInput & keys) == keys and (currentInput & keys) == 0; | ||
} | ||
|
||
pub inline fn isKeyUp(keys: u16) bool { | ||
return (currentInput & keys) == 0; | ||
} | ||
}; |
Oops, something went wrong.