eco2d/code/game/src/utils/raylib_helpers.h

153 lines
5.4 KiB
C
Raw Normal View History

2021-05-12 14:42:22 +00:00
#pragma once
#include "system.h"
#include "raylib.h"
2021-05-12 16:28:39 +00:00
#include "world/blocks.h"
2021-05-13 12:17:44 +00:00
#include "assets.h"
2021-05-12 14:42:22 +00:00
2021-08-11 17:38:24 +00:00
#include "rlgl.h"
2021-05-12 14:42:22 +00:00
static inline
void DrawTextEco(const char *text, float posX, float posY, int fontSize, Color color, float spacing) {
#if 1
// Check if default font has been loaded
if (GetFontDefault().texture.id != 0) {
Vector2 position = { (float)posX , (float)posY };
int defaultFontSize = 10; // Default Font chars height in pixel
int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing;
DrawTextEx(GetFontDefault(), text, position, (float)fontSize , (float)new_spacing , color);
}
#endif
}
static inline
int MeasureTextEco(const char *text, int fontSize, float spacing) {
#if 1
Vector2 vec = { 0.0f, 0.0f };
// Check if default font has been loaded
if (GetFontDefault().texture.id != 0) {
int defaultFontSize = 10; // Default Font chars height in pixel
int new_spacing = spacing == 0.0f ? fontSize/defaultFontSize : spacing;
vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)new_spacing);
}
return (int)vec.x;
#else
return 0;
#endif
}
static inline
void DrawCircleEco(float centerX, float centerY, float radius, Color color)
{
DrawCircleV((Vector2){ (float)centerX , (float)centerY }, radius , color);
}
static inline
2021-05-15 11:43:29 +00:00
void DrawRectangleEco(float posX, float posY, float width, float height, Color color)
2021-05-12 14:42:22 +00:00
{
2021-05-15 11:43:29 +00:00
DrawRectangleV((Vector2){ (float)posX , (float)posY }, (Vector2){ width , height }, color);
2021-05-12 14:42:22 +00:00
}
2021-05-12 16:28:39 +00:00
static inline
Texture2D GetBlockImage(uint8_t id) {
return *(Texture2D*)blocks_get_img(id);
2021-05-12 17:26:41 +00:00
}
2021-07-18 18:30:27 +00:00
static inline
RenderTexture2D GetChunkTexture(uint64_t id) {
RenderTexture2D *tex = (RenderTexture2D*)blocks_get_chunk_tex(id);
if (!tex) return (RenderTexture2D){0};
2021-07-18 18:30:27 +00:00
return *tex;
}
2021-05-12 17:26:41 +00:00
static inline
Image GetSpriteImage(uint16_t id) {
return *(Image*)assets_get_tex(id);
}
static inline
Sound GetSound(uint16_t id) {
return *(Sound*)assets_get_snd(id);
2021-08-11 17:38:24 +00:00
}
// Draw cube
// NOTE: Cube position is the center position
static inline
void EcoDrawCube(Vector3 position, float width, float height, float length, float heading, Color color)
{
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
rlCheckRenderBatchLimit(36);
rlPushMatrix();
// NOTE: Transformation is applied in inverse order (scale -> rotate -> translate)
rlTranslatef(position.x, position.y, position.z);
rlRotatef(heading, 0, 1, 0);
//rlScalef(1.0f, 1.0f, 1.0f); // NOTE: Vertices are directly scaled on definition
rlBegin(RL_TRIANGLES);
rlColor4ub(color.r, color.g, color.b, color.a);
// Front face
rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left
rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Right
rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
// Back face
rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Left
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right
rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right
rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
// Top face
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
rlVertex3f(x - width/2, y + height/2, z + length/2); // Bottom Left
rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right
rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left
rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right
// Bottom face
rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Left
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left
rlVertex3f(x + width/2, y - height/2, z - length/2); // Top Right
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right
rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Left
// Right face
rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right
rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right
rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left
rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Left
rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right
rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left
// Left face
rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right
rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left
rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Right
rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left
rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left
rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right
rlEnd();
rlPopMatrix();
}