revamp GridMap

This commit is contained in:
sillysagiri 2023-09-08 21:05:30 +07:00
parent 9afe1d0993
commit 419aa7cc2c
2 changed files with 56 additions and 44 deletions

42
src/utils/GridMap.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "GridMap.hpp"
void DrawGridMap(Texture texture, Camera2D cam)
{
Vector2 topLeft = GetScreenToWorld2D({0.0f, 0.0f}, cam);
Vector2 botRight = GetScreenToWorld2D({(float)GetScreenWidth(), (float)GetScreenHeight()}, cam);
float offsetX = (int)topLeft.x % texture.width;
float offsetY = (int)topLeft.y % texture.width;
DrawTextureTiled(
texture,
{offsetX, offsetY, (float)texture.width, (float)texture.height},
{topLeft.x, topLeft.y, botRight.x-topLeft.x, botRight.y-topLeft.y},
{0.0f, 0.0f}, 0.0f, 1.0f, WHITE
);
}
void DrawGridMapQuick(Texture texture, Vector2 offset)
{
float offsetX = (int)offset.x % texture.width;
float offsetY = (int)offset.y % texture.width;
DrawTextureTiled(
texture,
{offsetX, offsetY, (float)texture.width, (float)texture.height},
{offset.x, offset.y, (float)GetScreenWidth(), (float)GetScreenHeight()},
{0.0f, 0.0f}, 0.0f, 1.0f, WHITE
);
}
Texture LoadDefaultGridMap()
{
Image image = GenImageColor(100, 100, WHITE);
ImageDrawLine(&image, image.width*0.5f, 0, image.width*0.5f, image.height, {0, 0, 0, 120});
ImageDrawLine(&image, 0, image.height*0.5f, 100, image.height*0.5f, {0, 0, 0, 120});
ImageDrawRectangleLines(&image, {0.0f, 0.0f, (float)image.width, (float)image.height}, 1, {0, 0, 0, 180});
Texture tex = LoadTextureFromImage(image);
UnloadImage(image);
return tex;
}

View File

@ -1,50 +1,20 @@
#pragma once
#include <cmath>
#include "utils/RaylibDeprecated.hpp"
#include <raylib.h>
#include "utils/RaylibDeprecated.hpp"
/**
* Draw GridMap using cam, use this inside BeginMode2D
* @param {Texture} texture :
* @param {Camera2D} cam :
*/
void DrawGridMap(Texture texture, Camera2D cam);
class GridMap
{
public:
GridMap()
{
Image image = GenImageColor(100, 100, WHITE);
ImageDrawLine(&image, image.width*0.5f, 0, image.width*0.5f, image.height, {0, 0, 0, 120});
ImageDrawLine(&image, 0, image.height*0.5f, 100, image.height*0.5f, {0, 0, 0, 120});
ImageDrawRectangleLines(&image, {0.0f, 0.0f, image.width - 2.0f, image.height - 2.0f}, 2, {0, 0, 0, 180});
/**
* This is quick version without cam, does not support zoom
* @param {Texture} texture :
* @param {Camera2D} cam :
*/
void DrawGridMapQuick(Texture texture, Vector2 offset);
texture = LoadTextureFromImage(image);
UnloadImage(image);
}
~GridMap()
{
UnloadTexture(texture);
}
void Draw(Camera2D camera)
{
Vector2 topLeft = GetScreenToWorld2D({0.0f, 0.0f}, camera);
Vector2 botRight = GetScreenToWorld2D({(float)GetScreenWidth(), (float)GetScreenHeight()}, camera);
float SWidth = (std::ceil((botRight.x-topLeft.x) / texture.width) + 1) * texture.width;
float SHeight = (std::ceil((botRight.y-topLeft.y) / texture.height) + 1) * texture.height;
float SX = (std::ceil(topLeft.x / texture.width) - 1) * texture.width;
float SY = (std::ceil(topLeft.y / texture.height) - 1) * texture.height;
DrawTextureTiled(
texture,
{0.0f, 0.0f, (float)texture.width, (float)texture.height},
{SX, SY, SWidth, SHeight},
// {topLeft.x, topLeft.y, botRight.x-topLeft.x, botRight.y-topLeft.y},
{0.0f, 0.0f}, 0.0f, 1.0f, WHITE
);
}
private:
Texture2D texture;
};
Texture LoadDefaultGridMap();