Skip to main content
2 of 4
added clarity

Having a Logic Error in my tile rendering code

I am a novice programmer.

In my program, I can render a 5000 x 500 map, but it renders incorrectly, instead of this :

,,~,,,,~,,,,,~,~,,,,,~~,,,~,,,~,,,~,,~~~,~~~,,,~,,,,~,~,,,~~,~,,~,~,~~,~,,,,,~,,~~,,~,,,,,,~,,,,,~,,~~,~,,,,,,,~,,,,~,~~,,,~,,,~,~~~,,~~~,,,,,,,,~,~~,,,,,~,,,,,~~,,,~,,~~,~~~,,,~,,~,~,,~,,,~~,~~,~,,~~~~~,~,,,,,~,~,~,~,,,,~~,,~,,,,,~,,,~,,~,,~,,,,~~,,,~,,,,,,,~,~,,,,,~,,,,~,~~,~,,,,,,,~,~~,,,,,~~,,,,,,~,~~~~,,~,,~,,,,~~,,,,~~,~~~,,~,~,,~~~~,,,~,,~,,,,~~,~,,,,,,,,,,,~,,~~,,~,~,~,,,,~~,,,,,,,,,~,,,~,,,,~~~,,,~,,~,,,,,~,,~,~~,,~~,,,,~,,~,,,,,,,,,,,,~~,,,,,,,,~,,~~,,,,~,,,~,,,,~,,,,~,,,,,,~,,,,,,~,~,,,,,,,~,,~~~,~~~~,,~~,~,~,~,,,~,~,,~,~~,~,,,,,,~,~,~,,,,~,,,~,,~,,~,,,,,~~~~,,,,,~,,,~~,,,~,,,,~~,~,~~,~,~~,~,~,~~~~,,,~,,,,,,~,,,,,,,~,,,,,~~,~~~,,~~,~~,~,,~,,~~~,,,,,,~~,~,~,,,,~,,,,,,,,,,~~,,,,~,,~,,~~,,,~,,,,~,~,,~,~,,,~~~~,~,,,,,,~~~,,~,,~~~,~~~,~,,~,,,~,,,,,,~~,,,,~,,,~~~,,~,~,,,,~~,~,,,,,,~,~,,~~~,,~~,,,~,,,,~,,~,~,,,,,,,,,~~,,,,,,~~,~~,,,~~~,,~,~,~,,,~~,,~,,,,,,,,,~,~,,~,,,,,,,~,,~,,~~~,~~,~,,,,~,~,,,,,,~,,,~,,,,,,,,,~,,~~~~,,,~~,,~,~,,~,,,,,,~~~~~~,,,~,,,,,~,,,,,,~,~,,,~~~,~,,~,~,,,,,,~,~,~,,,,,,~,,,~~,~,,,,,~~,~,~,,~,~~,,~,,,~,,,~~~,,,,,~,,~,~,,,~,~,,~~~~~~,,,,~,,,,,,,,,~,,,~,~~,,,,,,,,~,~~,,,,,~,,~~,,~,~~,,,,,~,,,~,,~~,~,,~,,,,,,,,,~~,~~,~,,,~,,~~,~~,,,~~,,~~~~,,,,,,,,~,,,,,~,~,,

which was generated using a random coin flip

it renders the tiles in weird lines like this: image of rendered tilemap

Here is the link to the source code https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4f7290e72b64183ada9908ae50e782c5

Here is the rendering code:

    // Tile Renderer. 

    let screen_w_in_tiles: f32 = ceilf(screen_width() / 32.0);
    let screen_h_in_tiles: f32 = ceilf(screen_height() / 32.0);

    let center_tile_x = ((screen_width() / 2.0) - worldx as f32) / 32.0;
    let center_tile_y = ((screen_height() / 2.0) - worldy as f32) / 32.0;

    let lo_tile_x = floorf(center_tile_x - screen_w_in_tiles / 2.0); 
    let hi_tile_x = floorf(center_tile_x + screen_w_in_tiles / 2.0 + 2.0); 

    let lo_tile_y = ceilf(center_tile_y - screen_h_in_tiles / 2.0 - 2.0); 
    let hi_tile_y = ceilf(center_tile_y + screen_h_in_tiles / 2.0); 

    for i in lo_tile_x as usize..hi_tile_x as usize {
        for j in lo_tile_y as usize..hi_tile_y as usize {
            match world_data[i] {
                ','=> tile = grass,
                '*'=> tile = sand,
                '~'=> tile = water,
                '/'=> tile = ice,
                _=>   tile = unknown,
            }
            blit(tile, i as isize * 32 + worldx, j as isize * 32 + worldy, 32, 32);
        }
    }