Skip to content

Commit

Permalink
command buffer working
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitslime committed Jul 16, 2018
1 parent b65d725 commit e58e725
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 91 deletions.
Binary file removed res/font.bmp
Binary file not shown.
Binary file added res/terminal16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions res/text.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ void main() {

in vec2 uv;
uniform sampler2D font;
out vec4 color;
uniform vec3 color;
out vec4 out_color;

void main() {
vec4 sample = texture(font, uv);
color = mix(vec4(sample.rgb,0), sample, sample.r * 2);
vec4 sample = texture(font, uv) * vec4(color, 1);
out_color = mix(vec4(sample.rgb,0), sample, sample.r * 2);
}

#endif
6 changes: 3 additions & 3 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <memory.h>

void buffer_alloc(buffer_t *dest, size_t size) {
void *data = calloc(size, 1);
void *data = malloc(size);
if (data == NULL) {
dest->data = NULL;
dest->cap = 0;
Expand All @@ -29,6 +29,6 @@ void *buffer_getSpace(buffer_t *buffer, size_t size) {
}
}
void buffer_empty(buffer_t *buffer) {
memset(buffer->data, 0, buffer->cap);
// memset(buffer->data, 0, buffer->cap);
buffer->size = 0;
}
}
155 changes: 124 additions & 31 deletions src/cmd.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cmd.h"
#include "flagship.h"
#include "cvar.h"
#include "buffer.h"
#include "console.h"
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -10,6 +12,9 @@
int cmd_argc = 0;
char *cmd_argv[MAX_ARGV] = {NULL};

#define MAX_COMMAND_BUFFER 8192
static buffer_t command_buffer;

static unsigned long hash_str(const char *string) {
unsigned long out = 5381, c;

Expand Down Expand Up @@ -54,40 +59,89 @@ cmd_t *cmd_find(const char *name) {
return var;
}

void cmd_exec(const char *command) {
char buffer[1024];
strncpy(buffer, command, 1023);
char *runner = buffer;
void cmd_addString(const char *string) {
size_t length = strlen(string);
char *buffer = buffer_getSpace(&command_buffer, length + 1);

while (*runner) {
int i, quotes = 0;
for (i = 0; runner[i]; i++) {
if (runner[i] == '"') {
if (buffer == NULL) {
con_print("COMMAND BUFFER OVERFLOW!\n");
return;
}

memcpy(buffer, string, length);
buffer[length] = '\n';
}

static bool_t command_buffer_wait = false;

void cmd_execBuffer(void) {
char *buffer = (char*)command_buffer.data;
char line[1024];
int i = 0, start = 0;

while (i < command_buffer.size) {

if (command_buffer_wait) {
memcpy(buffer, buffer + i, command_buffer.size - i);
command_buffer.size -= i;
command_buffer_wait = false;
return;
}

bool_t quotes = false;
for (; i < command_buffer.size; i++) {
if (buffer[i] == '"') {
quotes = !quotes;
} else if (runner[i] == ';' && !quotes) {
runner[i] = '\0';
} else if (buffer[i] == ';' && !quotes){
break;
} else if (buffer[i] == '\n') {
break;
}
}
cmd_tokenize(runner);
runner += i + 1;

if (!cmd_argc) {
return;
}
memcpy(line, buffer + start, i - start);
line[i - start] = '\0';
cmd_exec(line);

cvar_t *cvar;
cmd_t *cmd;
start = ++i;
}

buffer_empty(&command_buffer);
}

void cmd_execFile(const char *fileName) {
FILE *file = fopen(fileName, "r");

if (file == NULL) {
con_printf("!could not open file %s!\n", fileName);
}

fseek(file, 0, SEEK_END);
unsigned long length = ftell(file);
rewind(file);

char *buffer = malloc(length + 1);
fread(buffer, sizeof(char), length, file);
buffer[length] = '\0';

cmd_addString(buffer);
free(buffer);
}

void cmd_exec(const char *command) {
char buffer[1024];
strncpy(buffer, command, 1024);

cmd_tokenize(buffer);

if ((cvar = cvar_find(cmd_argv[0]))) {
if (cmd_argc == 1) {
printf("%s\n", cvar->value);
} else {
cvar_setc(cvar, cmd_argv[1]);
}
} else if ((cmd = cmd_find(cmd_argv[0]))) {
cmd->func();
}
if (!cmd_argc) {
return;
}

cmd_t *cmd = cmd_find(cmd_argv[0]);

if (cmd != NULL) {
cmd->func();
}
}

Expand Down Expand Up @@ -136,21 +190,60 @@ void cmd_tokenize(char *buffer) {
}
}

static void cmd_quit(void) {
static void cmd_quit_f(void) {
exit(0);
}

static void cmd_echo(void) {
static void cmd_echo_f(void) {
for (int i = 1; i < cmd_argc; i++) {
con_printf("%s ", cmd_argv[i]);
}
con_print("\n");
}

static cmd_t quit = {"quit", cmd_quit};
static cmd_t echo = {"echo", cmd_echo};
static void cmd_set_f(void) {
if (cmd_argc == 1) {
con_printf("?set <cvar> <value>\n");
} else {
cvar_t *var = cvar_find(cmd_argv[1]);

if (var == NULL) {
con_printf("!could not find \"%s\"\n", cmd_argv[1]);
return;
}

if (cmd_argc == 2) {
con_printf("%s: %s\n", var->name, var->value);
} else {
cvar_set(cmd_argv[1], cmd_argv[2]);
}
}
}

static void cmd_wait_f(void) {
command_buffer_wait = true;
}

static void cmd_exec_f(void) {
if (cmd_argc < 2) {
con_print("?exec <file name>\n");
} else {
cmd_execFile(cmd_argv[1]);
}
}

static cmd_t quit = {"quit", cmd_quit_f};
static cmd_t echo = {"echo", cmd_echo_f};
static cmd_t set = {"set", cmd_set_f};
static cmd_t wait = {"wait", cmd_wait_f};
static cmd_t exec = {"exec", cmd_exec_f};

void cmd_init(void) {
cmd_register(&quit);
cmd_register(&echo);
}
cmd_register(&set);
cmd_register(&wait);
cmd_register(&exec);

buffer_alloc(&command_buffer, MAX_COMMAND_BUFFER);
}
6 changes: 5 additions & 1 deletion src/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ extern char *cmd_argv[MAX_ARGV];
void cmd_register(cmd_t *cmd);
cmd_t *cmd_find(const char *name);

void cmd_addString(const char *string);
void cmd_execBuffer(void);

void cmd_execFile(const char *fileName);
void cmd_exec(const char *command);
void cmd_tokenize(char *string);

void cmd_init(void);
void cmd_init(void);
37 changes: 25 additions & 12 deletions src/console.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "console.h"
#include "flagship.h"
#include "cvar.h"
#include "cmd.h"
#include "text.h"
#include "buffer.h"
Expand All @@ -15,17 +17,18 @@ static char input_buffer[MAX_INPUT_BUFFER + 1] = {0};
#define MAX_CONSOLE_BUFFER 8192
static buffer_t console_buffer;

#define MAX_LINES 12
#define MAX_LINES 30
#define MAX_LINE_LENGTH 128
static int console_backtrack = 0;
static char console_lines[MAX_LINES][MAX_LINE_LENGTH];
static int viewable_lines = MAX_LINES;

static void con_clear(void) {
buffer_empty(&console_buffer);
}

static void con_cls(void) {
for (int i = 0; i < MAX_LINES; i++) {
for (int i = 0; i < viewable_lines; i++) {
con_print("\n");
}
}
Expand All @@ -37,10 +40,10 @@ void con_init(void) {
cmd_register(&clear);
cmd_register(&cls);
buffer_alloc(&console_buffer, MAX_CONSOLE_BUFFER);
con_printf("%s\n", glGetString(GL_RENDERER));
}

void con_draw(int w, int h) {
viewable_lines = min(h / 38, MAX_LINES);

for (int i = 0; i < MAX_LINES; i++) {
memset(console_lines[i], 0, MAX_LINE_LENGTH);
Expand All @@ -54,7 +57,7 @@ void con_draw(int w, int h) {
size--;
}

int line = MAX_LINES-1;
int line = viewable_lines-1;
for (int i = size, end = size; line >= 0; i--) {
if (i <= 0) {
memcpy(console_lines[line], buffer, end + 1);
Expand Down Expand Up @@ -83,19 +86,27 @@ void con_draw(int w, int h) {
}
}

for (int i = 0; i < MAX_LINES; i++) {
text_draw(console_lines[i], 0, (float)i);
text_setColor(1, 1, 1);
for (int i = 0; i < viewable_lines; i++) {
char *line = console_lines[i];
switch (*line) {
case '!' : text_setColor(1, 0, 0); line++; break;
case '?' : text_setColor(0.6f, 0.6f, 0.6f); line++; break;
}
text_draw(line, 0, (float)i);
text_setColor(1, 1, 1);
}


if (((int)glfwGetTime()) % 2) {
text_draw("_", (float)strlen(input_buffer), (float)MAX_LINES);
if (sin(glfwGetTime() * 4) > 0 ) {
text_draw("\xDD", (float)strlen(input_buffer) + 1.8f, (float)viewable_lines);
}
text_draw(input_buffer, 0, (float)MAX_LINES);
text_draw("\xAF", 0, (float)viewable_lines);
text_draw(input_buffer, 1.5f, (float)viewable_lines);
}

static void con_exec_input(void) {
cmd_exec(input_buffer);
cmd_addString(input_buffer);
input_cursor = 0;
memset(input_buffer, 0, MAX_INPUT_BUFFER);
}
Expand Down Expand Up @@ -125,7 +136,9 @@ void con_print(const char *string) {
char *buffer = buffer_getSpace(&console_buffer, length);

if (buffer == NULL) {
printf("o shit\n");
//TODO: dump console buffer to file
buffer_empty(&console_buffer);
con_print("!console buffer overflow!\n");
return;
}

Expand Down Expand Up @@ -156,4 +169,4 @@ void con_seekDown(void) {

char *con_buffer(void) {
return (char*)console_buffer.data;
}
}
6 changes: 6 additions & 0 deletions src/flagship.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#define max(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) > (b) ? (b) : (a))

typedef enum {false, true} bool_t;
Loading

0 comments on commit e58e725

Please sign in to comment.