Commit 8b8d5c51 authored by Benjamin REED's avatar Benjamin REED

w.i.p. rework of asset system

parent f7d43721
No preview for this file type
...@@ -5,6 +5,8 @@ import std::core::mem; ...@@ -5,6 +5,8 @@ import std::core::mem;
import libc; import libc;
import std::thread; import std::thread;
import std::collections::list; import std::collections::list;
import std::collections::map;
import std::core::types;
import raylib; import raylib;
import rendering; import rendering;
...@@ -14,6 +16,7 @@ import room_gen; ...@@ -14,6 +16,7 @@ import room_gen;
def SpriteList = List(<Sprite*>); def SpriteList = List(<Sprite*>);
def EntityList = List(<Entity>); def EntityList = List(<Entity>);
def IndexList = List(<usz>); def IndexList = List(<usz>);
def ImageList = List(<Image>);
const Colour RED = {255, 0, 0, 255}; const Colour RED = {255, 0, 0, 255};
const Colour WHITE = {255, 255, 255, 255}; const Colour WHITE = {255, 255, 255, 255};
...@@ -71,8 +74,6 @@ const double MAP_AABB_RAYCAST_CHECK_RAY_STEP = 0.05; ...@@ -71,8 +74,6 @@ const double MAP_AABB_RAYCAST_CHECK_RAY_STEP = 0.05;
const uint COLUMN_PIXEL_WIDTH = 1; const uint COLUMN_PIXEL_WIDTH = 1;
const uint PIXEL_HEIGHT = 1; const uint PIXEL_HEIGHT = 1;
const uint TEXTURE_COUNT = 1;
// DEBUG // DEBUG
bool draw_minimap = false; bool draw_minimap = false;
...@@ -80,34 +81,72 @@ bool draw_floor_and_ceiling = true; ...@@ -80,34 +81,72 @@ bool draw_floor_and_ceiling = true;
//------ //------
const usz WORLD_TEXTURES_COUNT = 1; const usz WORLD_TEXTURES_COUNT = 0;
const usz SPRITE_TEXTURES_COUNT = 1; const usz SPRITE_TEXTURES_COUNT = 0;
const usz WEAPON_TEXTURES_COUNT = 1; const usz WEAPON_TEXTURES_COUNT = 0;
// NOTE: this 'usz' actually means 'AssetCategory', bug(?) in the
// hashmap implementation means i can't use it
def AssetMap = HashMap(<usz, any>); // yucky, can't use
// enum even tho its usz
struct Assets { struct Assets {
Image* world_textures; AssetMap assets;
Image* sprite_textures;
Image* weapon_textures;
Image missing; Image missing;
} }
macro Assets.@get(&self, category, idx) { enum AssetCategory : usz (typeid type) {
switch (category) { WORLD_TEXTURES = Image.typeid,
case "ws": SPRITE_TEXTURES = Image.typeid,
case "weapons": WEAPON_TEXTURES = Image.typeid,
}
fn void Assets.init(Assets this) {
this.assets.new_init();
}
fn usz Assets.category_len(Assets self, AssetCategory category) {
any possible_list = self.assets.get((usz)category)!!;
switch (possible_list) {
case ImageList:
return possible_list.len();
default:
return 0;
}
}
fn Image* Assets.missing(Assets* self) {
return &self.missing;
}
// macro so that Asset struct can return more than just an image pointer (not used yet)
macro Assets.@get(&self, $Type, category, idx) {
if (try category_ls = self.assets.get( (usz)category )) {
if ($Type.typeid == category_ls.type.inner) {
io::printfn("Attempt to index into an asset that is not a list.");
return &self.missing;
}
else {
return &anycast(category_ls, $Type)!![idx];
}
}
else {
return &self.missing;
}
/*switch (category) {
case AssetCategory.WEAPON_TEXTURES:
if (idx < 0 || idx >= WEAPON_TEXTURES_COUNT) return &self.missing; if (idx < 0 || idx >= WEAPON_TEXTURES_COUNT) return &self.missing;
return &self.weapon_textures[idx]; return &self.weapon_textures[idx];
break; break;
case "w": case AssetCategory.WORLD_TEXTURES:
case "wrld":
case "world":
if (idx < 0 || idx >= WORLD_TEXTURES_COUNT) return &self.missing; if (idx < 0 || idx >= WORLD_TEXTURES_COUNT) return &self.missing;
return &self.world_textures[idx]; return &self.world_textures[idx];
break; break;
case "s": case AssetCategory.SPRITE_TEXTURES:
case "sprite":
if (idx < 0 || idx >= SPRITE_TEXTURES_COUNT) return &self.missing; if (idx < 0 || idx >= SPRITE_TEXTURES_COUNT) return &self.missing;
return &self.sprite_textures[idx]; return &self.sprite_textures[idx];
...@@ -115,24 +154,35 @@ macro Assets.@get(&self, category, idx) { ...@@ -115,24 +154,35 @@ macro Assets.@get(&self, category, idx) {
default: default:
return &self.missing; return &self.missing;
} }
*/
return &self.missing; return &self.missing;
} }
fn Image* Assets.load_images(Assets this, String[] paths) { fn void Assets.load_images(Assets this, AssetCategory category, String[] image_paths) {
if (!this.assets.has_key( (usz) category)) {
io::printn("TODO: don't do this, make it similar to the '@get' interface, and make the sizes not be constants because that's a bit schewpid in the year of our lord twenty twenty four"); ImageList* list = (ImageList*)calloc(ImageList.sizeof);
this.assets.set( (usz)category, list );
}
foreach (path: paths) { foreach (path: image_paths) {
// TODO: don't do this, make it similar to the '@get' Image img;
// interface, and make the sizes not be constants because that's a img = raylib::load_image(path);
// bit schewpid in the year of our lord twenty twenty four any val = this.assets.get( (usz)category )!!;
switch (val.type) {
case ImageList:
((ImageList*)val).push(img);
break;
default:
io::printfn("WARNING: Type mismatch error pushing images to Assets.");
break;
}
} }
return null;
} }
fn void Assets.free(Assets this) { fn void Assets.free(Assets this) {
io::printfn("TODO: IMPLEMENT FREEING ASSETS"); io::printfn("TODO: IMPLEMENT FREEING ASSETS");
this.assets.free();
//for (int i = 0; i < TEXTURE_COUNT; i ++) raylib::unload_image(textures[i]); //for (int i = 0; i < TEXTURE_COUNT; i ++) raylib::unload_image(textures[i]);
} }
...@@ -474,10 +524,11 @@ fn int main() { ...@@ -474,10 +524,11 @@ fn int main() {
world.map = (MapCell*)calloc(MAP_WIDTH * MAP_HEIGHT * MapCell.sizeof); world.map = (MapCell*)calloc(MAP_WIDTH * MAP_HEIGHT * MapCell.sizeof);
Assets assets; Assets assets;
assets.init();
assets.missing = raylib::load_image("assets/missing.png"); assets.missing = raylib::load_image("assets/missing.png");
assets.weapon_textures = assets.load_images({"assets/weapons/pistol.png"}); assets.load_images(WEAPON_TEXTURES, {"assets/weapons/pistol.png"});
assets.world_textures = assets.load_images({"assets/col_test.png","assets/brick_wall.png"}); assets.load_images(WORLD_TEXTURES, {"assets/col_test.png","assets/brick_wall.png"});
assets.sprite_textures = assets.load_images({"assets/sprites/barrel.png","assets/sprites/boy.png"}); assets.load_images(SPRITE_TEXTURES, {"assets/sprites/barrel.png","assets/sprites/boy.png"});
/*Image* textures = (Image*)calloc(TEXTURE_COUNT * Image.sizeof); /*Image* textures = (Image*)calloc(TEXTURE_COUNT * Image.sizeof);
String[TEXTURE_COUNT] image_paths = { String[TEXTURE_COUNT] image_paths = {
"assets/col_test.png", "assets/col_test.png",
......
...@@ -148,10 +148,10 @@ fn void raycast_walls(Player* player, MapCell* map, Assets* assets, Colour* pixe ...@@ -148,10 +148,10 @@ fn void raycast_walls(Player* player, MapCell* map, Assets* assets, Colour* pixe
if (map[map_index] != 0) z_buffer[i] = perp_wall_dist; if (map[map_index] != 0) z_buffer[i] = perp_wall_dist;
int texture_index = map[map_index]; int texture_index = map[map_index];
if (texture_index <= main::TEXTURE_COUNT) { if (texture_index <= assets.category_len(AssetCategory.WORLD_TEXTURES)) {
texture_index--; texture_index--;
Image *texture = assets.@get("w", texture_index); Image *texture = assets.@get(Image, AssetCategory.WORLD_TEXTURES, texture_index);
double wall_x = 0.0; double wall_x = 0.0;
if (side == 0) { if (side == 0) {
...@@ -242,14 +242,14 @@ fn void raycast_floors(Player* player, MapCell* map, Assets* assets, Colour* pix ...@@ -242,14 +242,14 @@ fn void raycast_floors(Player* player, MapCell* map, Assets* assets, Colour* pix
for (int x = 0; x < main::RENDER_WINDOW_WIDTH; x ++) { for (int x = 0; x < main::RENDER_WINDOW_WIDTH; x ++) {
int[<2>] cell = (int[<2>])math::floor(floor_pos); int[<2>] cell = (int[<2>])math::floor(floor_pos);
int floor_tex_idx = 1; int floor_tex_idx = 0;
int ceiling_tex_idx = 1; int ceiling_tex_idx = 0;
if (cell.x % 2 == 0 && cell.y % 2 == 0) { if (cell.x % 2 == 0 && cell.y % 2 == 0) {
floor_tex_idx = 1; floor_tex_idx = 1;
} }
Image* floor_texture = assets.@get("w", floor_tex_idx); Image* floor_texture = assets.missing();//assets.@get(ImageList, AssetCategory.WORLD_TEXTURES, floor_tex_idx);
Image* ceiling_texture = assets.@get("w", ceiling_tex_idx); Image* ceiling_texture = assets.missing();//assets.@get(ImageList, AssetCategory.WORLD_TEXTURES, ceiling_tex_idx);
// TODO: calculate separately for different sized floor & ceiling textures // TODO: calculate separately for different sized floor & ceiling textures
int[<2>] tex_pos = { int[<2>] tex_pos = {
...@@ -349,7 +349,7 @@ fn void render_sprites(Player* player, SpriteList* sprites, Assets* assets, Colo ...@@ -349,7 +349,7 @@ fn void render_sprites(Player* player, SpriteList* sprites, Assets* assets, Colo
draw_end_x = main::RENDER_WINDOW_WIDTH - 1; draw_end_x = main::RENDER_WINDOW_WIDTH - 1;
} }
Image* tex = assets.@get("sprite", sprite.texture_index); Image* tex = assets.missing();//assets.@get(ImageList, AssetCategory.SPRITE_TEXTURES, sprite.texture_index);
for (int stripe = draw_start_x; stripe < draw_end_x; stripe++) { for (int stripe = draw_start_x; stripe < draw_end_x; stripe++) {
int tex_x = (int) (256 * (stripe - (-sprite_width / 2 + sprite_screen_x)) * tex.width / sprite_width) / 256; int tex_x = (int) (256 * (stripe - (-sprite_width / 2 + sprite_screen_x)) * tex.width / sprite_width) / 256;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment