Add highlighting, fixed len instructions and more
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
#include "cpustatusui.h"
|
||||
#include "font.h"
|
||||
|
||||
SDL_Texture *renderVals(CPU *cpu, BitmapFont *titleFont, BitmapFont *valueFont,
|
||||
SDL_Renderer *renderer) {
|
||||
void renderVals(CPU *cpu, BitmapFont *titleFont, BitmapFont *valueFont,
|
||||
SDL_Renderer *renderer, SDL_Texture **out) {
|
||||
|
||||
CPUStatusPart *stats = NULL;
|
||||
int statsCount = 0;
|
||||
@@ -16,12 +16,14 @@ SDL_Texture *renderVals(CPU *cpu, BitmapFont *titleFont, BitmapFont *valueFont,
|
||||
getStats(cpu, &stats, &statsCount);
|
||||
|
||||
const int padding = 4;
|
||||
const int oneFieldW = (titleFont->size + 1) * (sizeof(stats[0].value) - 1) + padding - 1;
|
||||
const int oneFieldW = (titleFont->size + 1) * (sizeof(stats[0].value)) + padding - 1;
|
||||
const int oneFieldH = (titleFont->size + 1) * 2 + (valueFont->size + 1);
|
||||
SDL_Texture *out = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
||||
oneFieldW * statsCount, oneFieldH);
|
||||
if (!*out) {
|
||||
*out = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
||||
oneFieldW * statsCount, oneFieldH);
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(renderer, out);
|
||||
SDL_SetRenderTarget(renderer, *out);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
@@ -61,7 +63,7 @@ SDL_Texture *renderVals(CPU *cpu, BitmapFont *titleFont, BitmapFont *valueFont,
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
return out;
|
||||
free(stats);
|
||||
}
|
||||
|
||||
void getStats(CPU *cpu, CPUStatusPart **cpuStatus, int *cpuStatusCount) {
|
||||
@@ -81,27 +83,27 @@ void getStats(CPU *cpu, CPUStatusPart **cpuStatus, int *cpuStatusCount) {
|
||||
|
||||
int index = 0;
|
||||
|
||||
strncpy((*cpuStatus)[index].name, "PC", sizeof((*cpuStatus)[index].name));
|
||||
strncpy((*cpuStatus)[index].name, "PCNT", sizeof((*cpuStatus)[index].name));
|
||||
(*cpuStatus)[index].value = cpu->pc;
|
||||
index++;
|
||||
|
||||
strncpy((*cpuStatus)[index].name, "SP", sizeof((*cpuStatus)[index].name));
|
||||
strncpy((*cpuStatus)[index].name, "SPTR", sizeof((*cpuStatus)[index].name));
|
||||
(*cpuStatus)[index].value = cpu->stack_ptr;
|
||||
index++;
|
||||
|
||||
strncpy((*cpuStatus)[index].name, "FLG", sizeof((*cpuStatus)[index].name));
|
||||
strncpy((*cpuStatus)[index].name, "FLAG", sizeof((*cpuStatus)[index].name));
|
||||
(*cpuStatus)[index].value = cpu->flags;
|
||||
index++;
|
||||
|
||||
strncpy((*cpuStatus)[index].name, "MOD", sizeof((*cpuStatus)[index].name));
|
||||
strncpy((*cpuStatus)[index].name, "MODE", sizeof((*cpuStatus)[index].name));
|
||||
(*cpuStatus)[index].value = cpu->mode;
|
||||
index++;
|
||||
|
||||
strncpy((*cpuStatus)[index].name, "CYC", sizeof((*cpuStatus)[index].name));
|
||||
strncpy((*cpuStatus)[index].name, "CYCL", sizeof((*cpuStatus)[index].name));
|
||||
(*cpuStatus)[index].value = cpu->cycle;
|
||||
index++;
|
||||
|
||||
for (int i = 0; i < 25; i++) {
|
||||
for (int i = 0; i < 18; i++) {
|
||||
snprintf((*cpuStatus)[index].name, sizeof((*cpuStatus)[index].name), "R%d", i);
|
||||
(*cpuStatus)[index].value = cpu->regs[i];
|
||||
index++;
|
||||
@@ -110,7 +112,7 @@ void getStats(CPU *cpu, CPUStatusPart **cpuStatus, int *cpuStatusCount) {
|
||||
*cpuStatusCount = index; // Store the actual number of status parts
|
||||
}
|
||||
|
||||
SDL_Texture *renderState(CPU *cpu, BitmapFont *titleFont, SDL_Renderer *renderer) {
|
||||
void renderState(CPU *cpu, BitmapFont *titleFont, SDL_Renderer *renderer, SDL_Texture **out) {
|
||||
|
||||
// Render the value
|
||||
char valueStr[20] = "";
|
||||
@@ -141,10 +143,12 @@ SDL_Texture *renderState(CPU *cpu, BitmapFont *titleFont, SDL_Renderer *renderer
|
||||
const int oneFieldW = (titleFont->size + 1);
|
||||
const int allFieldW = oneFieldW * strlen(valueStr);
|
||||
const int oneFieldH = (titleFont->size + 1);
|
||||
SDL_Texture *out = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
||||
allFieldW, oneFieldH);
|
||||
if (!*out) {
|
||||
*out = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
|
||||
allFieldW, oneFieldH);
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(renderer, out);
|
||||
SDL_SetRenderTarget(renderer, *out);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
@@ -164,5 +168,4 @@ SDL_Texture *renderState(CPU *cpu, BitmapFont *titleFont, SDL_Renderer *renderer
|
||||
}
|
||||
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
return out;
|
||||
}
|
@@ -9,14 +9,14 @@
|
||||
#include "../cpu/core.h"
|
||||
|
||||
typedef struct {
|
||||
char name[4];
|
||||
char name[5];
|
||||
uint32_t value;
|
||||
} CPUStatusPart;
|
||||
|
||||
SDL_Texture *renderVals(CPU *cpu, BitmapFont *titleFont, BitmapFont *valueFont,
|
||||
SDL_Renderer *renderer);
|
||||
void renderVals(CPU *cpu, BitmapFont *titleFont, BitmapFont *valueFont,
|
||||
SDL_Renderer *renderer, SDL_Texture ** out);
|
||||
|
||||
SDL_Texture *renderState(CPU *cpu, BitmapFont *titleFont, SDL_Renderer *renderer);
|
||||
void renderState(CPU *cpu, BitmapFont *titleFont, SDL_Renderer *renderer, SDL_Texture **out);
|
||||
|
||||
void getStats(CPU *cpu, CPUStatusPart **cpuStatus, int *cpuStatusCount);
|
||||
|
||||
|
@@ -12,8 +12,12 @@ prepText(SDL_Renderer *renderer, unsigned char pxSize, const char *file, uint8_t
|
||||
out.color = (SDL_Color) {r, g, b, a};
|
||||
unsigned int i = 1;
|
||||
do {
|
||||
if (i == 173) { //specifically this char is 0 width (IDK why)
|
||||
out.surface[i] = SDL_CreateRGBSurface(0,pxSize,pxSize,32,0,0,0,0);
|
||||
} else {
|
||||
char tmpOut[2] = {i, 0};
|
||||
out.surface[i] = TTF_RenderText_Solid(gFont, tmpOut, out.color);
|
||||
}
|
||||
out.texture[i] = SDL_CreateTextureFromSurface(renderer, out.surface[i]);
|
||||
i++;
|
||||
} while (i < 256);
|
||||
|
@@ -6,44 +6,39 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <malloc.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BYTES_PER_LINE 16 // Adjust for different widths
|
||||
|
||||
|
||||
char *hexdump_to_string(const unsigned char *data, size_t size) {
|
||||
char *hexdump_to_string(const unsigned char *data, uint32_t start, uint32_t end) {
|
||||
if (start >= end) return NULL; // Invalid range
|
||||
|
||||
// Estimate max output size: each line is approx. 80 chars
|
||||
size_t estimated_size = (size / BYTES_PER_LINE + 1) * 80;
|
||||
uint32_t estimated_size = ((end - start) / BYTES_PER_LINE + 1) * 60;
|
||||
|
||||
// Allocate memory for output string
|
||||
char *output = malloc(estimated_size);
|
||||
if (!output) return NULL;
|
||||
|
||||
size_t offset = 0; // Track the write position
|
||||
uint32_t out_offset = 0; // Tracks position in output buffer
|
||||
|
||||
for (size_t i = 0; i < size; i += BYTES_PER_LINE) {
|
||||
offset += snprintf(output + offset, estimated_size - offset, "%08zx ", i);
|
||||
for (uint32_t i = start; i < end; i += BYTES_PER_LINE) {
|
||||
// Print offset
|
||||
out_offset += snprintf(output + out_offset, estimated_size - out_offset, "%07x ", i);
|
||||
|
||||
// Print hex values
|
||||
for (size_t j = 0; j < BYTES_PER_LINE; j++) {
|
||||
if (i + j < size)
|
||||
offset += snprintf(output + offset, estimated_size - offset, "%02x ", data[i + j]);
|
||||
for (uint32_t j = 0; j < BYTES_PER_LINE; j++) {
|
||||
if (i + j < end)
|
||||
out_offset += snprintf(output + out_offset, estimated_size - out_offset, "%02x ", data[i + j]);
|
||||
else
|
||||
offset += snprintf(output + offset, estimated_size - offset, " "); // Padding
|
||||
if (j == 7) offset += snprintf(output + offset, estimated_size - offset, " "); // Extra space
|
||||
out_offset += snprintf(output + out_offset, estimated_size - out_offset, " "); // Padding
|
||||
if (j == 7)
|
||||
out_offset += snprintf(output + out_offset, estimated_size - out_offset, " "); // Extra space
|
||||
}
|
||||
|
||||
offset += snprintf(output + offset, estimated_size - offset, " |");
|
||||
|
||||
// Print ASCII representation
|
||||
for (size_t j = 0; j < BYTES_PER_LINE; j++) {
|
||||
if (i + j < size)
|
||||
offset += snprintf(output + offset, estimated_size - offset, "%c",
|
||||
isprint(data[i + j]) ? data[i + j] : '.');
|
||||
else
|
||||
offset += snprintf(output + offset, estimated_size - offset, " ");
|
||||
}
|
||||
|
||||
offset += snprintf(output + offset, estimated_size - offset, "|\n");
|
||||
out_offset += snprintf(output + out_offset, estimated_size - out_offset, "\n");
|
||||
}
|
||||
|
||||
return output;
|
||||
|
@@ -6,7 +6,8 @@
|
||||
#define RISCB_HEXDUMP_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
char *hexdump_to_string(const unsigned char *data, size_t size);
|
||||
char *hexdump_to_string(const unsigned char *data, uint32_t start, uint32_t end);
|
||||
|
||||
#endif //RISCB_HEXDUMP_H
|
||||
|
@@ -5,13 +5,14 @@
|
||||
|
||||
#include "texteditor.h"
|
||||
#include "font.h"
|
||||
#include "../cpu/core.h"
|
||||
|
||||
// Initialize the text editor with dynamic sizes.
|
||||
void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Renderer *renderer,
|
||||
int max_line_width, int max_lines_asm, int max_lines_display, bool readOnly) {
|
||||
editor->max_line_width = max_line_width;
|
||||
editor->max_lines_asm = max_lines_asm;
|
||||
editor->max_lines_display = max_lines_display;
|
||||
editor->maxLines = max_lines_asm;
|
||||
editor->displayLineCount = max_lines_display;
|
||||
editor->line_count = 0;
|
||||
editor->cursor_line = 0;
|
||||
editor->cursor_line_offset = 0;
|
||||
@@ -22,20 +23,22 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
|
||||
editor->outRect = malloc(sizeof(SDL_Rect));
|
||||
editor->cursorRect = malloc(sizeof(SDL_Rect));
|
||||
editor->rect = malloc(sizeof(SDL_Rect));
|
||||
editor->highlightedLineRect = malloc(sizeof(SDL_Rect));
|
||||
|
||||
memset(editor->outRect, 0, sizeof(SDL_Rect));
|
||||
memset(editor->cursorRect, 0, sizeof(SDL_Rect));
|
||||
memset(editor->rect, 0, sizeof(SDL_Rect));
|
||||
memset(editor->highlightedLineRect, 0, sizeof(SDL_Rect));
|
||||
|
||||
|
||||
// Allocate dynamic array for lines.
|
||||
editor->lines = (Line *) malloc(sizeof(Line) * editor->max_lines_asm);
|
||||
editor->lines = (Line *) malloc(sizeof(Line) * editor->maxLines);
|
||||
if (!editor->lines) {
|
||||
fprintf(stderr, "Failed to allocate memory for lines.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// For each line, allocate memory for the text (including space for '\0')
|
||||
for (int i = 0; i < editor->max_lines_asm; i++) {
|
||||
for (int i = 0; i < editor->maxLines; i++) {
|
||||
editor->lines[i].text = (char *) malloc(sizeof(char) * (editor->max_line_width + 1));
|
||||
if (!editor->lines[i].text) {
|
||||
fprintf(stderr, "Failed to allocate memory for line %d.\n", i);
|
||||
@@ -46,8 +49,8 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
|
||||
}
|
||||
|
||||
// Allocate output and display strings.
|
||||
editor->outputString = (char *) malloc(sizeof(char) * (editor->max_line_width * editor->max_lines_asm + 1));
|
||||
editor->displayString = (char *) malloc(sizeof(char) * (editor->max_line_width * editor->max_lines_display + 1));
|
||||
editor->outputString = (char *) malloc(sizeof(char) * (editor->max_line_width * editor->maxLines + 1));
|
||||
editor->displayString = (char *) malloc(sizeof(char) * (editor->max_line_width * editor->displayLineCount + 1));
|
||||
if (!editor->outputString || !editor->displayString) {
|
||||
fprintf(stderr, "Failed to allocate memory for output/display strings.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
@@ -60,21 +63,26 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
|
||||
editor->lines[1].active = 1;
|
||||
editor->line_count = 2;
|
||||
|
||||
// Set up the editor rectangle based on font size and dynamic max_line_width and max_lines_display.
|
||||
// Set up the editor rectangle based on font size and dynamic max_line_width and displayLineCount.
|
||||
editor->rect->x = 2;
|
||||
editor->rect->y = 2;
|
||||
editor->rect->w = editor->max_line_width * (font->size + 1) + ((font->size + 1) / 2);
|
||||
editor->rect->h = editor->max_lines_display * (font->size + 1) + 2;
|
||||
editor->rect->h = editor->displayLineCount * (font->size + 1) + 2;
|
||||
editor->outRect->w = editor->rect->w;
|
||||
editor->outRect->h = editor->rect->h;
|
||||
editor->outRect->x = x;
|
||||
editor->outRect->y = y;
|
||||
|
||||
editor->highlightedLineRect->x = 2;
|
||||
editor->highlightedLineRect->y = 2;
|
||||
editor->highlightedLineRect->w = editor->outRect->w - (2 * editor->highlightedLineRect->x);
|
||||
editor->highlightedLineRect->h = editor->font->size;
|
||||
|
||||
editor->cursorRect->x = 3 + editor->cursor_pos * font->size + editor->outRect->x;
|
||||
editor->cursorRect->y =
|
||||
2 + (editor->cursor_line - editor->cursor_line_offset) * (font->size + 1) + editor->outRect->y;
|
||||
|
||||
editor->cursorRect->w = 2;
|
||||
editor->cursorRect->w = editor->font->size;
|
||||
editor->cursorRect->h = editor->font->size;
|
||||
|
||||
// Create texture for rendering.
|
||||
@@ -87,9 +95,8 @@ void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Rendere
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a new line at a specific position.
|
||||
void insert_line(TextEditor *editor, int position, const char *text, SDL_Renderer *renderer) {
|
||||
if (editor->line_count >= editor->max_lines_asm || position < 0 || position > editor->line_count) {
|
||||
if (editor->line_count >= editor->maxLines || position < 0 || position > editor->line_count) {
|
||||
printf("Invalid position or max lines reached!\n");
|
||||
return;
|
||||
}
|
||||
@@ -111,7 +118,16 @@ void insert_line(TextEditor *editor, int position, const char *text, SDL_Rendere
|
||||
}
|
||||
|
||||
void insert_line_rel(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
insert_line(editor, editor->cursor_line + (editor->cursor_pos ? 1 : 0), "", renderer);
|
||||
int line = editor->cursor_line;
|
||||
int pos = editor->cursor_pos;
|
||||
char *current_text = editor->lines[line].text;
|
||||
|
||||
// Split the current line at the cursor position
|
||||
char new_line_text[editor->max_line_width + 1];
|
||||
strcpy(new_line_text, ¤t_text[pos]); // Copy the text after cursor to the new line
|
||||
current_text[pos] = '\0'; // Truncate the current line at cursor position
|
||||
|
||||
insert_line(editor, line + 1, new_line_text, renderer);
|
||||
editor->cursor_pos = 0;
|
||||
}
|
||||
|
||||
@@ -154,11 +170,27 @@ void remove_character(TextEditor *editor, bool isDelete, SDL_Renderer *renderer)
|
||||
for (int i = editor->cursor_pos; i < len; i++) {
|
||||
line->text[i] = line->text[i + 1];
|
||||
}
|
||||
} else if (editor->cursor_pos == len && editor->cursor_line < editor->line_count - 1) {
|
||||
// Merge next line with the current line when pressing delete at the end
|
||||
strcat(line->text, editor->lines[editor->cursor_line + 1].text);
|
||||
|
||||
// Shift remaining lines up
|
||||
for (int i = editor->cursor_line + 1; i < editor->line_count - 1; i++) {
|
||||
strcpy(editor->lines[i].text, editor->lines[i + 1].text);
|
||||
editor->lines[i].active = editor->lines[i + 1].active;
|
||||
}
|
||||
editor->lines[editor->line_count - 1].text[0] = '\0';
|
||||
editor->lines[editor->line_count - 1].active = 0;
|
||||
editor->line_count--;
|
||||
}
|
||||
} else {
|
||||
// Backspace behavior (delete character before cursor)
|
||||
if (editor->cursor_pos == 0 && editor->line_count > 1) {
|
||||
// Merge with the previous line
|
||||
if (editor->cursor_pos == 0 && editor->cursor_line > 0) {
|
||||
// Append current line to previous line
|
||||
int prev_len = strlen(editor->lines[editor->cursor_line - 1].text);
|
||||
strcat(editor->lines[editor->cursor_line - 1].text, line->text);
|
||||
|
||||
// Shift lines up
|
||||
for (int i = editor->cursor_line; i < editor->line_count - 1; i++) {
|
||||
strcpy(editor->lines[i].text, editor->lines[i + 1].text);
|
||||
editor->lines[i].active = editor->lines[i + 1].active;
|
||||
@@ -166,10 +198,10 @@ void remove_character(TextEditor *editor, bool isDelete, SDL_Renderer *renderer)
|
||||
editor->lines[editor->line_count - 1].text[0] = '\0';
|
||||
editor->lines[editor->line_count - 1].active = 0;
|
||||
editor->line_count--;
|
||||
if (editor->cursor_line >= editor->line_count) {
|
||||
editor->cursor_line = editor->line_count - 1;
|
||||
}
|
||||
editor->cursor_pos = strlen(editor->lines[editor->cursor_line].text);
|
||||
|
||||
// Move cursor to the end of the previous line
|
||||
editor->cursor_line--;
|
||||
editor->cursor_pos = prev_len;
|
||||
} else if (editor->cursor_pos > 0) {
|
||||
for (int i = editor->cursor_pos - 1; i < len; i++) {
|
||||
line->text[i] = line->text[i + 1];
|
||||
@@ -191,7 +223,14 @@ void move_cursor_relative(TextEditor *editor, int line_offset, int pos_offset, b
|
||||
|
||||
void move_cursor(TextEditor *editor, int new_line, int new_pos, bool keepPos, SDL_Renderer *renderer) {
|
||||
if (new_line < 0) new_line = 0;
|
||||
if (new_line >= editor->line_count) new_line = editor->line_count - 1;
|
||||
int trgt = editor->line_count - editor->displayLineCount;
|
||||
if (trgt < 0) {
|
||||
trgt = 0;
|
||||
}
|
||||
if (new_line >= trgt && new_line >= editor->line_count) {
|
||||
new_line = editor->line_count -
|
||||
1;
|
||||
}
|
||||
|
||||
if (keepPos) {
|
||||
editor->cursor_line_offset = new_line;
|
||||
@@ -200,8 +239,15 @@ void move_cursor(TextEditor *editor, int new_line, int new_pos, bool keepPos, SD
|
||||
if (new_line < editor->cursor_line_offset) {
|
||||
editor->cursor_line_offset = new_line;
|
||||
}
|
||||
if (new_line >= editor->cursor_line_offset + editor->max_lines_display) {
|
||||
editor->cursor_line_offset = new_line - editor->max_lines_display + 1;
|
||||
if (new_line >= editor->cursor_line_offset + editor->displayLineCount) {
|
||||
editor->cursor_line_offset = new_line - editor->displayLineCount + 1;
|
||||
}
|
||||
|
||||
if (editor->cursor_line_offset >= editor->line_count - editor->displayLineCount) {
|
||||
editor->cursor_line_offset = editor->line_count - editor->displayLineCount;
|
||||
}
|
||||
if (editor->cursor_line_offset < 0) {
|
||||
editor->cursor_line_offset = 0;
|
||||
}
|
||||
|
||||
int line_length = strlen(editor->lines[new_line].text);
|
||||
@@ -219,7 +265,7 @@ void generate_string_display(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
int end_line = editor->cursor_line_offset + editor->max_lines_display;
|
||||
int end_line = editor->cursor_line_offset + editor->displayLineCount;
|
||||
if (end_line > editor->line_count)
|
||||
end_line = editor->line_count;
|
||||
|
||||
@@ -227,7 +273,7 @@ void generate_string_display(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
editor->displayString[0] = '\0';
|
||||
|
||||
SDL_SetRenderTarget(renderer, editor->texture);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
SDL_Rect charDstRect;
|
||||
@@ -263,7 +309,8 @@ void generate_string_display(TextEditor *editor, SDL_Renderer *renderer) {
|
||||
SDL_SetRenderTarget(renderer, NULL);
|
||||
}
|
||||
|
||||
void editor_render(TextEditor *editor, SDL_Renderer *renderer, bool isActive, bool cursorBlink) {
|
||||
void editor_render(TextEditor *editor, SDL_Renderer *renderer, CPU *cpu, uint8_t editorIndex, bool isActive,
|
||||
bool cursorBlink) {
|
||||
if (isActive) {
|
||||
SDL_Rect bgRect;
|
||||
bgRect = *editor->outRect;
|
||||
@@ -271,20 +318,57 @@ void editor_render(TextEditor *editor, SDL_Renderer *renderer, bool isActive, bo
|
||||
bgRect.y -= 6;
|
||||
bgRect.w += 12;
|
||||
bgRect.h += 12;
|
||||
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, editor->readOnly ? 128 : 0, editor->readOnly ? 0 : 128, 64, 255);
|
||||
SDL_RenderFillRect(renderer, &bgRect);
|
||||
|
||||
}
|
||||
SDL_RenderCopy(renderer, editor->texture, editor->rect, editor->outRect);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderFillRect(renderer, editor->outRect);
|
||||
|
||||
uint32_t targetLine = 0;
|
||||
const uint32_t instrLine = cpu->pc / CPU_INSTRUCTION_SIZE;
|
||||
if (editorIndex == 0) {
|
||||
targetLine = cpu->addrToLineMapper[instrLine] - editor->cursor_line_offset;
|
||||
editor->highlightedLineRect->w = editor->rect->w - 1;
|
||||
editor->highlightedLineRect->x = 2;
|
||||
}
|
||||
if (editorIndex == 1) {
|
||||
targetLine = (instrLine / 2) - editor->cursor_line_offset;
|
||||
editor->highlightedLineRect->w = (editor->rect->w / 5 * 2) - 2;
|
||||
editor->highlightedLineRect->x =
|
||||
2 + (11 * editor->font->size) + (instrLine % 2 ? (28 * editor->font->size) : 0);
|
||||
|
||||
}
|
||||
editor->highlightedLineRect->y = 2 + ((editor->font->size + 1) * targetLine);
|
||||
SDL_Rect highlightedLineAbs = *editor->highlightedLineRect;
|
||||
highlightedLineAbs.x += editor->outRect->x;
|
||||
highlightedLineAbs.y += editor->outRect->y;
|
||||
SDL_SetRenderDrawColor(renderer, 0, 72, 64, 255);
|
||||
if (targetLine >= editor->cursor_line_offset &&
|
||||
targetLine < editor->cursor_line_offset + editor->displayLineCount) {
|
||||
SDL_RenderFillRect(renderer, &highlightedLineAbs);
|
||||
}
|
||||
|
||||
|
||||
SDL_SetTextureBlendMode(editor
|
||||
->texture, SDL_BLENDMODE_BLEND);
|
||||
SDL_RenderCopy(renderer, editor
|
||||
->texture, editor->rect, editor->outRect);
|
||||
if (isActive && cursorBlink) {
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
|
||||
SDL_RenderFillRect(renderer, editor->cursorRect);
|
||||
SDL_SetRenderDrawColor(renderer,
|
||||
0, 255, 255, 128);
|
||||
SDL_RenderFillRect(renderer, editor
|
||||
->cursorRect);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void generate_string(TextEditor *editor) {
|
||||
editor->outputString[0] = '\0';
|
||||
|
||||
for (int i = 0; i < editor->max_lines_asm; i++) {
|
||||
for (int i = 0; i < editor->maxLines; i++) {
|
||||
if (editor->lines[i].active) {
|
||||
if (strlen(editor->lines[i].text)) {
|
||||
strcat(editor->outputString, editor->lines[i].text);
|
||||
@@ -294,30 +378,39 @@ void generate_string(TextEditor *editor) {
|
||||
}
|
||||
}
|
||||
|
||||
void fill_editor_from_string(TextEditor *editor, const char *content, SDL_Renderer *renderer) {
|
||||
void fill_editor_from_string(TextEditor *editor, const char *content, int lineStart, bool isComplete,
|
||||
SDL_Renderer *renderer) {
|
||||
if (!editor || !content) {
|
||||
printf("Invalid editor or content pointer!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *str = content;
|
||||
|
||||
int numLines = 0;
|
||||
while (*str) if (*str++ == '\n') ++numLines;
|
||||
|
||||
// Clear the current editor content
|
||||
for (int i = 0; i < editor->max_lines_asm; i++) {
|
||||
for (int i = lineStart; i < editor->maxLines && i < lineStart + numLines + 1; i++) {
|
||||
editor->lines[i].text[0] = '\0';
|
||||
editor->lines[i].active = 0;
|
||||
}
|
||||
editor->line_count = 0;
|
||||
editor->cursor_line = 0;
|
||||
editor->cursor_pos = 0;
|
||||
if (isComplete) {
|
||||
editor->line_count = numLines;
|
||||
if (editor->line_count >= editor->maxLines) {
|
||||
editor->line_count = editor->maxLines - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the content and fill the editor lines
|
||||
const char *ptr = content;
|
||||
int line_index = 0;
|
||||
int line_index = lineStart;
|
||||
|
||||
while (*ptr && line_index < editor->max_lines_asm) {
|
||||
while (*ptr && line_index < editor->maxLines && line_index < numLines + lineStart) {
|
||||
int char_count = 0;
|
||||
|
||||
// Ensure the text buffer does not overflow
|
||||
while (*ptr && *ptr != '\n' && char_count < editor->max_line_width - 1) {
|
||||
while (*ptr && *ptr != '\n' && char_count < editor->max_line_width) {
|
||||
editor->lines[line_index].text[char_count++] = *ptr++;
|
||||
}
|
||||
|
||||
@@ -328,9 +421,11 @@ void fill_editor_from_string(TextEditor *editor, const char *content, SDL_Render
|
||||
// Move past the newline character if present
|
||||
if (*ptr == '\n') ptr++;
|
||||
}
|
||||
editor->lines[line_index].active = 1;
|
||||
|
||||
// Update the total number of lines in use
|
||||
editor->line_count = line_index;
|
||||
//editor->line_count = line_index + (line_index < editor->maxLines - 1 ? 1 : 0);
|
||||
//if (editor->line_count >= editor-> maxLines)
|
||||
|
||||
// Generate the visual representation
|
||||
generate_string_display(editor, renderer);
|
||||
@@ -340,13 +435,13 @@ void fill_editor_from_string(TextEditor *editor, const char *content, SDL_Render
|
||||
// Free all dynamically allocated memory.
|
||||
void destroy_editor(TextEditor *editor) {
|
||||
if (editor->lines) {
|
||||
for (int i = 0; i < editor->max_lines_asm; i++) {
|
||||
for (int i = 0; i < editor->maxLines; i++) {
|
||||
free(editor->lines[i].text);
|
||||
}
|
||||
free(editor->lines);
|
||||
}
|
||||
free(editor->outputString);
|
||||
free(editor->displayString);
|
||||
//free(editor->outputString);
|
||||
//free(editor->displayString);
|
||||
|
||||
free(editor->outRect);
|
||||
free(editor->rect);
|
||||
|
@@ -13,6 +13,7 @@
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include "font.h"
|
||||
#include "../cpu/core.h"
|
||||
|
||||
typedef struct {
|
||||
char *text; // Dynamically allocated string for this line
|
||||
@@ -22,25 +23,26 @@ typedef struct {
|
||||
typedef struct {
|
||||
Line *lines; // Dynamic array of lines
|
||||
int line_count; // Number of active lines
|
||||
int max_lines_asm; // Maximum number of lines (e.g. assembly lines)
|
||||
int maxLines; // Maximum number of lines (e.g. assembly lines)
|
||||
int max_line_width; // Maximum characters per line (excluding '\0')
|
||||
int max_lines_display; // Maximum number of lines for display
|
||||
int displayLineCount; // Maximum number of lines for display
|
||||
|
||||
int cursor_line; // Current cursor line
|
||||
int cursor_line_offset; // Display offset (first line in the display)
|
||||
int cursor_pos; // Current cursor position in line
|
||||
|
||||
char *outputString; // Dynamically allocated output string (size: max_line_width * max_lines_asm + 1)
|
||||
char *displayString; // Dynamically allocated display string (size: max_line_width * max_lines_display + 1)
|
||||
char *outputString; // Dynamically allocated output string (size: max_line_width * maxLines + 1)
|
||||
char *displayString; // Dynamically allocated display string (size: max_line_width * displayLineCount + 1)
|
||||
SDL_Rect *rect;
|
||||
SDL_Rect *outRect;
|
||||
SDL_Texture *texture;
|
||||
bool readOnly;
|
||||
BitmapFont *font;
|
||||
SDL_Rect *cursorRect;
|
||||
SDL_Rect *highlightedLineRect;
|
||||
} TextEditor;
|
||||
|
||||
// Initialize the text editor. The parameters max_line_width, max_lines_asm, and max_lines_display
|
||||
// Initialize the text editor. The parameters max_line_width, maxLines, and displayLineCount
|
||||
// determine the dynamic sizes for the text editor.
|
||||
void init_editor(TextEditor *editor, BitmapFont *font, int x, int y, SDL_Renderer *renderer,
|
||||
int max_line_width, int max_lines_asm, int max_lines_display, bool readOnly);
|
||||
@@ -50,7 +52,7 @@ void insert_line(TextEditor *editor, int position, const char *text, SDL_Rendere
|
||||
|
||||
void insert_line_rel(TextEditor *editor, SDL_Renderer *renderer);
|
||||
|
||||
void editor_render(TextEditor *editor, SDL_Renderer *renderer, bool isActive, bool cursorBlink);
|
||||
void editor_render(TextEditor *editor, SDL_Renderer *renderer, CPU *cpu, uint8_t editorIndex, bool isActive, bool cursorBlink);
|
||||
|
||||
void remove_character(TextEditor *editor, bool isDelete, SDL_Renderer *renderer);
|
||||
|
||||
@@ -64,7 +66,8 @@ void generate_string_display(TextEditor *editor, SDL_Renderer *renderer);
|
||||
|
||||
void generate_string(TextEditor *editor);
|
||||
|
||||
void fill_editor_from_string(TextEditor *editor, const char *content, SDL_Renderer *renderer);
|
||||
void fill_editor_from_string(TextEditor *editor, const char *content, int lineStart, bool isComplete,
|
||||
SDL_Renderer *renderer);
|
||||
|
||||
// A cleanup function to free dynamically allocated memory.
|
||||
void destroy_editor(TextEditor *editor);
|
||||
|
Reference in New Issue
Block a user