Font tweaks, start of editor, start of python tokenizer for syntax highlighting
This commit is contained in:
parent
322a0960d7
commit
f084c715bf
@ -42,10 +42,12 @@ add_executable(Pycron src/main.cpp
|
||||
src/States/GameState.h
|
||||
src/Graphics/Font.cpp
|
||||
src/Graphics/Font.h
|
||||
src/States/EditorState.cpp
|
||||
src/States/EditorState.h
|
||||
src/States/Editor/EditorState.cpp
|
||||
src/States/Editor/EditorState.h
|
||||
src/Graphics/PycronImage.h
|
||||
src/Graphics/PycronImage.cpp)
|
||||
src/Graphics/PycronImage.cpp
|
||||
src/States/Editor/PythonTokenizer.cpp
|
||||
src/States/Editor/PythonTokenizer.h)
|
||||
|
||||
add_subdirectory(dependencies/pocketpy)
|
||||
# Declaring our executable
|
||||
|
||||
@ -80,8 +80,8 @@
|
||||
"101": "00000000000111010001111111000001110",
|
||||
"102": "00000001110100011111010000100001000",
|
||||
"103": "00000000000111010001011110000101110",
|
||||
"104": "00000100001000011110100011000110001",
|
||||
"105": "00000001000000001100001000010000100",
|
||||
"104": "10000100001111010001100011000110001",
|
||||
"105": "00100000000110000100001000010000100",
|
||||
"106": "00000001000000001100001000010011000",
|
||||
"107": "00000100001001010100110001010010010",
|
||||
"108": "00000001000010000100001000010000010",
|
||||
@ -97,7 +97,7 @@
|
||||
"118": "00000000001000110001100010101000100",
|
||||
"119": "00000000001000110101101011010101010",
|
||||
"120": "00000000001000101010001000101010001",
|
||||
"121": "00000100011000110001011110000101110",
|
||||
"121": "00000000001000110001011110000101110",
|
||||
"122": "00000000001111100010001000100011111",
|
||||
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
//
|
||||
// Created by Bobby Lucero on 4/20/24.
|
||||
//
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include <random>
|
||||
#include <cmath>
|
||||
#include "Pycron.h"
|
||||
@ -11,17 +10,7 @@
|
||||
|
||||
std::string Pycron::PythonDirectory = "./python";
|
||||
|
||||
std::string loadFileToString(const std::string& filename) {
|
||||
std::ifstream file(filename); // Open the file
|
||||
std::stringstream buffer; // String stream to hold file content
|
||||
if (file.is_open()) { // Check if file is open
|
||||
buffer << file.rdbuf(); // Read the entire file into the buffer
|
||||
file.close(); // Close the file
|
||||
} else {
|
||||
std::cerr << "Unable to open file: " << filename << std::endl;
|
||||
}
|
||||
return buffer.str(); // Return the content string
|
||||
}
|
||||
|
||||
|
||||
Pycron::Pycron() {
|
||||
SetTraceLogLevel(LOG_ERROR);
|
||||
@ -153,3 +142,15 @@ pkpy::PyObject *Pycron::getMouseDown(pkpy::VM *vm, pkpy::ArgsView args) {
|
||||
return pkpy::py_var(vm, held);
|
||||
}
|
||||
|
||||
std::string Pycron::loadFileToString(const std::string &filename) {
|
||||
std::ifstream file(filename); // Open the file
|
||||
std::stringstream buffer; // String stream to hold file content
|
||||
if (file.is_open()) { // Check if file is open
|
||||
buffer << file.rdbuf(); // Read the entire file into the buffer
|
||||
file.close(); // Close the file
|
||||
} else {
|
||||
std::cerr << "Unable to open file: " << filename << std::endl;
|
||||
}
|
||||
return buffer.str(); // Return the content string
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include "pocketpy.h"
|
||||
#include "StateManager.h"
|
||||
#include "Graphics/Graphics.h"
|
||||
@ -37,5 +39,7 @@ public:
|
||||
static pkpy::PyObject* getKeyDown(pkpy::VM* vm, pkpy::ArgsView args);
|
||||
static pkpy::PyObject* getMousePressed(pkpy::VM* vm, pkpy::ArgsView args);
|
||||
static pkpy::PyObject* getMouseDown(pkpy::VM* vm, pkpy::ArgsView args);
|
||||
|
||||
static std::string loadFileToString(const std::string& filename);
|
||||
};
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ StateManager::StateManager(Pycron *pycron, Graphics *graphics) : m_pycron(pycron
|
||||
m_gameState = new GameState(m_pycron->m_vm, graphics);
|
||||
m_editorState = new EditorState(m_pycron->m_vm, graphics);
|
||||
m_currentState = nullptr;
|
||||
RequestStateChange(GAME);
|
||||
ChangeState(EDITOR);
|
||||
}
|
||||
|
||||
StateManager::~StateManager() {
|
||||
@ -19,7 +19,7 @@ StateManager::~StateManager() {
|
||||
}
|
||||
|
||||
|
||||
void StateManager::RequestStateChange(StateManager::StateType state) {
|
||||
void StateManager::ChangeState(StateManager::StateType state) {
|
||||
if(m_currentState){
|
||||
m_currentState->OnExit();
|
||||
}
|
||||
@ -46,9 +46,9 @@ void StateManager::Draw() {
|
||||
|
||||
if(IsKeyPressed(KEY_ENTER)){
|
||||
if(m_currentState == m_gameState){
|
||||
RequestStateChange(EDITOR);
|
||||
ChangeState(EDITOR);
|
||||
}else{
|
||||
RequestStateChange(GAME);
|
||||
ChangeState(GAME);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include "Pycron.h"
|
||||
#include "Graphics/Graphics.h"
|
||||
#include "States/GameState.h"
|
||||
#include "States/EditorState.h"
|
||||
#include "States/Editor/EditorState.h"
|
||||
|
||||
class Pycron;
|
||||
class Graphics;
|
||||
@ -29,7 +29,7 @@ public:
|
||||
explicit StateManager(Pycron* pycron, Graphics* graphics);
|
||||
~StateManager();
|
||||
|
||||
void RequestStateChange(StateType state);
|
||||
void ChangeState(StateManager::StateType state);
|
||||
|
||||
void RequestLoadGame();
|
||||
void RequestRunGame();
|
||||
|
||||
145
src/States/Editor/EditorState.cpp
Normal file
145
src/States/Editor/EditorState.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
//
|
||||
// Created by Bobby on 5/13/2024.
|
||||
//
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "EditorState.h"
|
||||
#include "../../Graphics/PycronImage.h"
|
||||
#include "../../Pycron.h"
|
||||
|
||||
|
||||
EditorState::EditorState(pkpy::VM *vm, Graphics *graphics) : m_vm(vm), m_graphics(graphics){
|
||||
m_pythonTokenizer = new PythonTokenizer();
|
||||
|
||||
Token a(TokenType::Keyword, "Test");
|
||||
|
||||
std::string randomSource = Pycron::loadFileToString("../python/main.py");
|
||||
|
||||
m_baseBackgroundColor = 59;
|
||||
m_baseTextColor = 51;
|
||||
m_lineNumberBackgroundColor = 58;
|
||||
m_lineNumberTextColor = 61;
|
||||
|
||||
m_topLetterSpacing = 1;
|
||||
m_bottomLetterSpacing = 1;
|
||||
m_leftLetterSpacing = 1;
|
||||
m_rightLetterSpacing = 1;
|
||||
|
||||
m_charWidth = m_leftLetterSpacing + m_graphics->GetCurrentFontWidth() + m_rightLetterSpacing; // Final size of char with spacing. If the literal width of the font is n, the final width is n + spacing.
|
||||
m_charHeight = m_topLetterSpacing + m_graphics->GetCurrentFontHeight() + m_bottomLetterSpacing;
|
||||
|
||||
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);
|
||||
m_foregroundIndexBuffer = std::vector<uint8_t>(m_textWidth * m_textHeight);
|
||||
m_backgroundIndexBuffer = std::vector<uint8_t>(m_textWidth * m_textHeight);
|
||||
|
||||
Clear();
|
||||
|
||||
|
||||
std::string testText = "This is a test\n[Test]\nThis is the end of the test.";
|
||||
|
||||
LoadStringToBuffer(randomSource);
|
||||
|
||||
}
|
||||
|
||||
EditorState::~EditorState() {
|
||||
delete m_pythonTokenizer;
|
||||
}
|
||||
|
||||
void EditorState::Draw() {
|
||||
m_graphics->Clear(0);
|
||||
|
||||
|
||||
for (int i = 0; i < m_characterBuffer.size(); ++i) {
|
||||
int x = (i % m_textWidth) * m_charWidth;
|
||||
int y = (i / m_textWidth) * m_charHeight;
|
||||
m_graphics->Rect(x, y, m_charWidth, m_charHeight, m_backgroundIndexBuffer[i]);
|
||||
m_graphics->Char(m_characterBuffer[i], x + m_leftLetterSpacing + 1, y + m_topLetterSpacing + 1, 0);
|
||||
m_graphics->Char(m_characterBuffer[i], x + m_leftLetterSpacing, y + m_topLetterSpacing, m_foregroundIndexBuffer[i]);
|
||||
}
|
||||
|
||||
if(m_dirty){
|
||||
Clear();
|
||||
m_dirty = false;
|
||||
for (int i = 0; i < m_text.size(); ++i) {
|
||||
std::string lineNumber = std::to_string(i);
|
||||
int size = 2;
|
||||
int diff = size - (int)lineNumber.size();
|
||||
if(diff > 0) lineNumber = std::string(diff, ' ') + lineNumber;
|
||||
Text(lineNumber, 0, i, m_lineNumberTextColor, m_lineNumberBackgroundColor);
|
||||
Text(m_text[i], size, i, m_baseTextColor, m_baseBackgroundColor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void EditorState::OnEnter() {
|
||||
|
||||
}
|
||||
|
||||
void EditorState::OnExit() {
|
||||
|
||||
}
|
||||
|
||||
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] = ' ';
|
||||
m_foregroundIndexBuffer[i] = m_baseTextColor;
|
||||
m_backgroundIndexBuffer[i] = m_baseBackgroundColor;
|
||||
}
|
||||
}
|
||||
|
||||
void EditorState::Text(const std::string &text, int x, int y, int fg, int bg) {
|
||||
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];
|
||||
int index = y * m_textWidth + (x + i);
|
||||
m_characterBuffer[index] = c;
|
||||
m_backgroundIndexBuffer[index] = bg;
|
||||
m_foregroundIndexBuffer[index] = fg;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void EditorState::OnCharPressed(char character) {
|
||||
|
||||
}
|
||||
|
||||
void EditorState::LoadStringToBuffer(const std::string &text) {
|
||||
m_text.clear();
|
||||
|
||||
|
||||
std::vector<std::string> lines;
|
||||
size_t start = 0;
|
||||
size_t end = 0;
|
||||
|
||||
while((end = text.find('\n', start)) != std::string::npos){
|
||||
lines.push_back(text.substr(start, end - start));
|
||||
start = end + 1;
|
||||
}
|
||||
|
||||
lines.push_back(text.substr(start));
|
||||
|
||||
for (const auto& line : lines) {
|
||||
m_text.push_back(line);
|
||||
}
|
||||
|
||||
m_dirty = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
53
src/States/Editor/EditorState.h
Normal file
53
src/States/Editor/EditorState.h
Normal file
@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <regex>
|
||||
#include "PythonTokenizer.h"
|
||||
#include "../../State.h"
|
||||
|
||||
|
||||
class PycronImage;
|
||||
|
||||
class EditorState : public State{
|
||||
|
||||
public:
|
||||
EditorState(pkpy::VM* vm, Graphics* graphics);
|
||||
~EditorState();
|
||||
|
||||
void Draw() override;
|
||||
void OnEnter() override;
|
||||
void OnExit() override;
|
||||
void OnKeyPressed(int key) override;
|
||||
void OnCharPressed(char character) override;
|
||||
|
||||
private:
|
||||
pkpy::VM* m_vm;
|
||||
Graphics* m_graphics;
|
||||
PythonTokenizer* m_pythonTokenizer;
|
||||
|
||||
uint8_t m_charWidth, m_charHeight;
|
||||
uint8_t m_textWidth, m_textHeight;
|
||||
uint8_t m_topLetterSpacing, m_bottomLetterSpacing, m_leftLetterSpacing, m_rightLetterSpacing;
|
||||
// Text Buffer
|
||||
std::vector<char> m_characterBuffer;
|
||||
std::vector<uint8_t> m_foregroundIndexBuffer;
|
||||
std::vector<uint8_t> m_backgroundIndexBuffer;
|
||||
|
||||
// Text Data
|
||||
std::vector<std::string> m_text;
|
||||
|
||||
// Theming
|
||||
uint8_t m_baseBackgroundColor;
|
||||
uint8_t m_baseTextColor;
|
||||
uint8_t m_lineNumberBackgroundColor;
|
||||
uint8_t m_lineNumberTextColor;
|
||||
|
||||
bool m_dirty;
|
||||
|
||||
void Clear();
|
||||
void Text(const std::string &text, int x, int y, int fg = 0, int bg = 63);
|
||||
|
||||
void LoadStringToBuffer(const std::string& text);
|
||||
|
||||
|
||||
};
|
||||
|
||||
5
src/States/Editor/PythonTokenizer.cpp
Normal file
5
src/States/Editor/PythonTokenizer.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by Bobby on 9/28/2024.
|
||||
//
|
||||
|
||||
#include "PythonTokenizer.h"
|
||||
29
src/States/Editor/PythonTokenizer.h
Normal file
29
src/States/Editor/PythonTokenizer.h
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by Bobby on 9/28/2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
enum TokenType{
|
||||
Keyword,
|
||||
Identifier,
|
||||
Literal,
|
||||
Operator,
|
||||
Punctuation,
|
||||
EndOfFile,
|
||||
Unknown
|
||||
};
|
||||
|
||||
struct Token{
|
||||
TokenType type;
|
||||
std::string value;
|
||||
|
||||
Token(TokenType t, const std::string& v)
|
||||
: type(t), value(v) {}
|
||||
};
|
||||
|
||||
class PythonTokenizer {
|
||||
|
||||
};
|
||||
@ -1,77 +0,0 @@
|
||||
//
|
||||
// Created by Bobby on 5/13/2024.
|
||||
//
|
||||
|
||||
#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() {
|
||||
delete test;
|
||||
}
|
||||
|
||||
void EditorState::Draw() {
|
||||
m_graphics->Clear(1);
|
||||
std::string text = "Editor State";
|
||||
|
||||
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);
|
||||
}
|
||||
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() {
|
||||
|
||||
}
|
||||
|
||||
void EditorState::OnExit() {
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
#include "../State.h"
|
||||
|
||||
class PycronImage;
|
||||
|
||||
class EditorState : public State{
|
||||
|
||||
public:
|
||||
EditorState(pkpy::VM* vm, Graphics* graphics);
|
||||
~EditorState();
|
||||
|
||||
void Draw() override;
|
||||
void OnEnter() override;
|
||||
void OnExit() 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);
|
||||
|
||||
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user