Added text buffer to editor state, foundation for the text editor

This commit is contained in:
Bobby Lucero 2024-09-28 01:01:54 -04:00
parent e23be1cd58
commit 322a0960d7
9 changed files with 96 additions and 26 deletions

View File

@ -74,7 +74,7 @@ void Graphics::renderVirtualScreen() {
BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(m_virtualScreen.texture, m_virtualScreenLocalBounds, m_virtualScreenWindowBounds, m_origin, 0.0f, WHITE);
DrawText(std::to_string(GetFPS()).c_str(), 10, 10, 30, YELLOW);
//DrawText(std::to_string(GetFPS()).c_str(), 10, 10, 30, YELLOW);
EndDrawing();
}
@ -358,8 +358,6 @@ void Graphics::Text(const std::string& s, int x, int y, int paletteIndex) {
for (int i = 0; i < s.size(); ++i) {
char c = s[i];
std::string bitData = m_currentFont->GetCharData((int)c);
//std::cout << c << ": " << (int)c << " = " << bitData << std::endl;
for (int j = 0; j < bitData.size(); ++j) {
if(bitData[j] == '1')
Pixel(x + (j % m_currentFont->GetWidth()) + ((m_currentFont->GetWidth() + 1) * i), y + (j / m_currentFont->GetWidth()), paletteIndex);
@ -368,6 +366,15 @@ void Graphics::Text(const std::string& s, int x, int y, int paletteIndex) {
}
}
void Graphics::Char(char c, int x, int y, int paletteIndex) {
std::string bitData = m_currentFont->GetCharData((int)c);
for (int j = 0; j < bitData.size(); ++j) {
if(bitData[j] == '1')
Pixel(x + (j % m_currentFont->GetWidth()), y + (j / m_currentFont->GetWidth()), paletteIndex);
}
}
void Graphics::updateVMVars(pkpy::VM* vm) {
vm->builtins->attr().set("mouseX", pkpy::py_var(vm, mouseX()));
vm->builtins->attr().set("mouseY", pkpy::py_var(vm, mouseY()));
@ -569,7 +576,12 @@ void Graphics::Img(PycronImage* img, int x, int y) {
}
}
int Graphics::GetCurrentFontWidth(){
return m_currentFont->GetWidth();
}
int Graphics::GetCurrentFontHeight(){
return m_currentFont->GetHeight();
}

View File

@ -22,14 +22,14 @@ private:
int m_windowWidth;
int m_windowHeight;
Rectangle m_virtualScreenWindowBounds; // size of rect texture on window
Vector2 m_origin; // position of rect texture on window
Rectangle m_virtualScreenLocalBounds; // virtual screen bounds
RenderTexture2D m_virtualScreen;
Rectangle m_virtualScreenWindowBounds; // size of rect texture on window
Vector2 m_origin; // position of rect texture on window
Rectangle m_virtualScreenLocalBounds; // virtual screen bounds
RenderTexture2D m_virtualScreen;
std::vector<uint8_t> m_virtualScreenColorBuffer;
Image m_virtualScreenImageBuffer;
PixelFont* m_currentFont;
PixelFont* m_NormalFont;
Image m_virtualScreenImageBuffer;
PixelFont* m_currentFont;
PixelFont* m_NormalFont;
private:
void renderVirtualScreen();
@ -76,9 +76,13 @@ public:
void Rect(int x, int y, int width, int height, int paletteIndex);
void RectBorder(int x, int y, int width, int height, int paletteIndex);
void Text(const std::string& s, int x, int y, int paletteIndex);
void Char(char c, int x, int y, int paletteIndex);
void Triangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, int paletteIndex);
void Img(PycronImage* img, int x, int y);
int GetPixel(int x, int y);
int GetCurrentFontWidth();
int GetCurrentFontHeight();
};

View File

@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <cstdint>
struct PycronImage {
int width;

View File

@ -109,8 +109,8 @@ pkpy::PyObject* Pycron::getRandomNumber(pkpy::VM* vm, pkpy::ArgsView args) {
float min = pkpy::py_cast<float>(vm, args[0]);
float max = pkpy::py_cast<float>(vm, args[1]);
// Seed the random number generator with a random device
std::random_device rd;
std::mt19937 gen(rd());
static std::random_device rd;
static std::mt19937 gen(rd());
// Define a uniform distribution for the range [min, max]
std::uniform_real_distribution<float> distribution(min, max);

View File

@ -11,5 +11,7 @@ public:
virtual void Draw() = 0;
virtual void OnEnter() = 0;
virtual void OnExit() = 0;
virtual void onKeyPressed(int key) = 0;
virtual void OnKeyPressed(int key) = 0;
virtual void OnCharPressed(char character) = 0;
};

View File

@ -5,8 +5,23 @@
#include "EditorState.h"
#include "../Graphics/PycronImage.h"
EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){
test = m_graphics->loadImage("resources/plane2.png");
m_horizontalLetterSpacing = 1;
m_verticalLetterSpacing = 1;
m_charWidth = m_graphics->GetCurrentFontWidth() + m_horizontalLetterSpacing; // Final size of char with spacing. If the literal width of the font is n, the final width is n + spacing.
m_charHeight = m_graphics->GetCurrentFontHeight() + m_verticalLetterSpacing;
m_textWidth = (int)(m_graphics->m_screenWidth / m_charWidth);
m_textHeight = (int)(m_graphics->m_screenHeight / m_charHeight);
m_characterBuffer = std::vector<char>(m_textWidth * m_textHeight);
Clear();
}
EditorState::~EditorState() {
@ -16,15 +31,13 @@ EditorState::~EditorState() {
void EditorState::Draw() {
m_graphics->Clear(1);
std::string text = "Editor State";
m_graphics->Text(text, m_graphics->m_screenWidth / 2 - (text.size() / 2 * 6), m_graphics->m_screenHeight / 2 - (7 / 2), 5);
for (int y = 0; y < 128; ++y) {
for (int x = 0; x < 128; ++x) {
int c = x;
m_graphics->Pixel(x, y, m_graphics->rgbToID(x * 2, y * 2, m_graphics->mouseX()));
}
for (int i = 0; i < m_characterBuffer.size(); ++i) {
m_graphics->Char(m_characterBuffer[i], (i % m_textWidth) * m_charWidth, (i / m_textWidth) * m_charHeight, 5);
}
std::cout << test->width << "\n";
m_graphics->Img(test, m_graphics->mouseX() - (test->height / 2), m_graphics->mouseY() - (test->width / 2));
Clear();
Text("This is a big long test!!!@%)(!*@#%PALSKDGJ", ((float)m_graphics->mouseX() / m_graphics->m_screenWidth) * m_textWidth, ((float)m_graphics->mouseY() / m_graphics->m_screenHeight) * m_textHeight);
}
void EditorState::OnEnter() {
@ -35,6 +48,30 @@ void EditorState::OnExit() {
}
void EditorState::onKeyPressed(int key) {
void EditorState::OnKeyPressed(int key) {
std::cout << key << ". \n";
}
void EditorState::Clear() {
for (int i = 0; i < m_textWidth * m_textHeight; ++i) {
m_characterBuffer[i] = ' ';
}
}
void EditorState::Text(const std::string &text, int x, int y) {
for (int i = 0; i < text.size(); ++i) {
int charX = x + i;
int charY = y;
if(charX < 0 || charY < 0 || charX >= m_textWidth || charY >= m_textHeight){
return;
}
char c = text[i];
m_characterBuffer[y * m_textWidth + (x + i)] = c;
}
}
void EditorState::OnCharPressed(char character) {
}

View File

@ -12,12 +12,20 @@ public:
void Draw() override;
void OnEnter() override;
void OnExit() override;
void onKeyPressed(int key) override;
void OnKeyPressed(int key) override;
void OnCharPressed(char character) override;
private:
pkpy::VM* m_vm;
PycronImage* test;
Graphics* m_graphics;
int m_charWidth, m_charHeight;
int m_textWidth, m_textHeight;
int m_horizontalLetterSpacing, m_verticalLetterSpacing;
std::vector<char> m_characterBuffer;
void Clear();
void Text(const std::string& text, int x, int y);
};

View File

@ -34,7 +34,7 @@ void GameState::OnExit() {
m_updateFunction = m_vm->None;
}
void GameState::onKeyPressed(int key) {
void GameState::OnKeyPressed(int key) {
}
@ -98,3 +98,7 @@ void GameState::loadPythonModules(std::unordered_map<std::string, std::string> &
}
}
void GameState::OnCharPressed(char character) {
}

View File

@ -14,7 +14,9 @@ public:
void Draw() override;
void OnEnter() override;
void OnExit() override;
void onKeyPressed(int key) override;
void OnKeyPressed(int key) override;
void OnCharPressed(char character) override;
private: