1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-22 07:13:07 +01:00

add in game console

This commit is contained in:
IntelOrca
2015-05-19 03:53:37 +01:00
parent 9b9338d90c
commit 8772cdd7aa
13 changed files with 451 additions and 27 deletions

View File

@@ -35,6 +35,7 @@
<ClCompile Include="..\src\game.c" />
<ClCompile Include="..\src\hook.c" />
<ClCompile Include="..\src\input.c" />
<ClCompile Include="..\src\interface\console.c" />
<ClCompile Include="..\src\interface\graph.c" />
<ClCompile Include="..\src\interface\keyboard_shortcut.c" />
<ClCompile Include="..\src\interface\screenshot.c" />
@@ -162,6 +163,7 @@
<ClInclude Include="..\src\game.h" />
<ClInclude Include="..\src\hook.h" />
<ClInclude Include="..\src\input.h" />
<ClInclude Include="..\src\interface\console.h" />
<ClInclude Include="..\src\interface\graph.h" />
<ClInclude Include="..\src\interface\keyboard_shortcut.h" />
<ClInclude Include="..\src\interface\screenshot.h" />

View File

@@ -369,7 +369,6 @@
<ClCompile Include="..\src\diagnostic.c">
<Filter>Source</Filter>
</ClCompile>
<ClCompile Include="..\lib\lodepng\lodepng.c" />
<ClCompile Include="..\src\windows\editor_bottom_toolbar.c">
<Filter>Source\Windows</Filter>
</ClCompile>
@@ -439,6 +438,9 @@
<ClCompile Include="..\src\windows\land_rights.c">
<Filter>Source\Windows</Filter>
</ClCompile>
<ClCompile Include="..\src\interface\console.c">
<Filter>Source\Interface</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\management\award.h">
@@ -642,5 +644,8 @@
<ClInclude Include="..\src\world\fountain.h">
<Filter>Source\World</Filter>
</ClInclude>
<ClInclude Include="..\src\interface\console.h">
<Filter>Source\Interface</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -4,7 +4,6 @@
<LocalDebuggerWorkingDirectory>$(TargetDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerCommand>$(TargetDir)\openrct2.exe</LocalDebuggerCommand>
<LocalDebuggerCommandArguments>
</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>"C:\Program Files (x86)\Infogrames\RollerCoaster Tycoon 2\Scenarios\Lucky Lake.SC6"</LocalDebuggerCommandArguments>
</PropertyGroup>
</Project>

View File

@@ -78,6 +78,7 @@ int cmdline_run(const char **argv, int argc)
if (argc != 0) {
gExitCode = cmdline_call_action(argv, argc);
if (gExitCode != 0)
return 0;
}

View File

@@ -25,6 +25,7 @@
#include "cursors.h"
#include "game.h"
#include "input.h"
#include "interface/console.h"
#include "interface/keyboard_shortcut.h"
#include "interface/viewport.h"
#include "interface/widget.h"
@@ -1140,6 +1141,7 @@ void game_handle_keyboard_input()
rct_window *w;
int key;
if (!gConsoleOpen) {
// Handle mouse scrolling
if (RCT2_GLOBAL(RCT2_ADDRESS_ON_TUTORIAL, uint8) == 0)
if (RCT2_GLOBAL(RCT2_ADDRESS_CONFIG_EDGE_SCROLLING, uint8) != 0)
@@ -1159,6 +1161,7 @@ void game_handle_keyboard_input()
if (RCT2_GLOBAL(RCT2_ADDRESS_ON_TUTORIAL, uint8) == 0)
game_handle_key_scroll();
}
}
// Handle key input
@@ -1166,6 +1169,16 @@ void game_handle_keyboard_input()
if (key == 255)
continue;
// Reserve backtick for console
if (key == SDL_SCANCODE_GRAVE) {
if (gConfigGeneral.debugging_tools || gConsoleOpen)
console_toggle();
continue;
} else if (gConsoleOpen) {
console_input(key);
continue;
}
key |= RCT2_GLOBAL(RCT2_ADDRESS_PLACE_OBJECT_MODIFIER, uint8) << 8;
w = window_find_by_class(WC_CHANGE_KEYBOARD_SHORTCUT);

373
src/interface/console.c Normal file
View File

@@ -0,0 +1,373 @@
#include <SDL_scancode.h>
#include "../addresses.h"
#include "../drawing/drawing.h"
#include "../localisation/localisation.h"
#include "../platform/platform.h"
#include "console.h"
#include "window.h"
#define CONSOLE_BUFFER_SIZE 1024
#define CONSOLE_HISTORY_SIZE 64
#define CONSOLE_INPUT_SIZE 256
bool gConsoleOpen = false;
static bool _consoleInitialised = false;
static int _consoleLeft, _consoleTop, _consoleRight, _consoleBottom;
static int _lastMainViewportX, _lastMainViewportY;
static char _consoleBuffer[CONSOLE_BUFFER_SIZE] = { 0 };
static char *_consoleBufferPointer = _consoleBuffer;
static char *_consoleViewBufferStart = _consoleBuffer;
static char _consoleCurrentLine[CONSOLE_INPUT_SIZE];
static char *_consoleCurrentLinePointer = _consoleCurrentLine;
static int _consoleCaretTicks;
static char _consoleHistory[CONSOLE_HISTORY_SIZE][CONSOLE_INPUT_SIZE];
static int _consoleHistoryIndex = 0;
static int _consoleHistoryCount = 0;
static void console_invalidate();
static void console_write_prompt();
static void console_update_scroll();
static void console_clear_input();
static void console_history_add(const char *src);
static void console_write_all_commands();
void console_open()
{
gConsoleOpen = true;
console_update_scroll();
platform_start_text_input(_consoleCurrentLine, sizeof(_consoleCurrentLine));
}
void console_close()
{
gConsoleOpen = false;
console_invalidate();
platform_stop_text_input();
}
void console_toggle()
{
if (gConsoleOpen)
console_close();
else
console_open();
}
void console_init()
{
_consoleInitialised = true;
console_writeline(OPENRCT2_NAME " " OPENRCT2_VERSION);
console_writeline("");
console_write_prompt();
}
void console_update()
{
if (!_consoleInitialised)
console_init();
_consoleLeft = 0;
_consoleTop = 0;
_consoleRight = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, sint16);
_consoleBottom = 300;
if (gConsoleOpen) {
console_invalidate();
// When scrolling the map, the console pixels get copied... therefore invalidate the screen
rct_window *mainWindow = window_get_main();
if (mainWindow != NULL) {
rct_viewport *mainViewport = mainWindow->viewport;
if (mainViewport != NULL) {
if (_lastMainViewportX != mainViewport->view_x || _lastMainViewportY != mainViewport->view_y) {
_lastMainViewportX = mainViewport->view_x;
_lastMainViewportY = mainViewport->view_y;
gfx_invalidate_screen();
}
}
}
// Remove unwated characters in console input
unsigned char *ch = (unsigned char*)_consoleCurrentLine;
while (*ch != 0) {
if (*ch < 32 || *ch > 126)
*ch = ' ';
ch++;
}
}
// Flash the caret
_consoleCaretTicks = (_consoleCaretTicks + 1) % 30;
}
void console_draw(rct_drawpixelinfo *dpi)
{
if (!gConsoleOpen)
return;
// Set font
RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, uint16) = 224;
// Background
gfx_fill_rect(dpi, _consoleLeft, _consoleTop, _consoleRight, _consoleBottom, 0);
int x = _consoleLeft + 4;
int y = _consoleTop + 4;
// Draw previous lines
char lineBuffer[1 + 256];
char *ch = _consoleViewBufferStart;
while (*ch != 0) {
// Find line break or null terminator
char *nextLine = ch;
while (*nextLine != 0 && *nextLine != '\n') {
nextLine++;
}
int lineLength = min(sizeof(lineBuffer) - 1, nextLine - ch);
strncpy(lineBuffer + 1, ch, lineLength);
lineBuffer[0] = FORMAT_GREEN;
lineBuffer[1 + lineLength] = 0;
gfx_draw_string(dpi, lineBuffer, 255, x, y);
x = gLastDrawStringX;
if (*nextLine == '\n') {
ch = nextLine + 1;
x = _consoleLeft + 4;
y += 10;
} else {
break;
}
}
// Draw current line
gfx_draw_string(dpi, _consoleCurrentLine, 255, x, y);
// Draw caret
if (_consoleCaretTicks >= 15) {
memcpy(lineBuffer, _consoleCurrentLine, gTextInputCursorPosition);
lineBuffer[gTextInputCursorPosition] = 0;
int caretX = x + gfx_get_string_width(lineBuffer);
int caretY = y + 10;
gfx_fill_rect(dpi, caretX, caretY, caretX + 6, caretY + 1, FORMAT_GREEN);
}
}
void console_input(int c)
{
switch (c) {
case SDL_SCANCODE_ESCAPE:
console_clear_input();
break;
case SDL_SCANCODE_RETURN:
if (_consoleCurrentLine[0] != 0) {
console_history_add(_consoleCurrentLine);
console_execute(_consoleCurrentLine);
console_write_prompt();
console_clear_input();
}
break;
case SDL_SCANCODE_UP:
if (_consoleHistoryIndex > 0) {
_consoleHistoryIndex--;
memcpy(_consoleCurrentLine, _consoleHistory[_consoleHistoryIndex], 256);
}
gTextInputCursorPosition = strlen(_consoleCurrentLine);
gTextInputLength = gTextInputCursorPosition;
break;
case SDL_SCANCODE_DOWN:
if (_consoleHistoryIndex < _consoleHistoryCount - 1) {
_consoleHistoryIndex++;
memcpy(_consoleCurrentLine, _consoleHistory[_consoleHistoryIndex], 256);
gTextInputCursorPosition = strlen(_consoleCurrentLine);
gTextInputLength = gTextInputCursorPosition;
} else {
_consoleHistoryIndex = _consoleHistoryCount;
console_clear_input();
}
break;
}
}
static void console_invalidate()
{
gfx_set_dirty_blocks(_consoleLeft, _consoleTop, _consoleRight, _consoleBottom);
}
static void console_write_prompt()
{
console_write("OpenRCT2 > ");
}
void console_write(const char *src)
{
int charactersRemainingInBuffer = CONSOLE_BUFFER_SIZE - (_consoleBufferPointer - _consoleBuffer) - 1;
int charactersToWrite = strlen(src);
int bufferShift = charactersToWrite - charactersRemainingInBuffer;
if (charactersToWrite > charactersRemainingInBuffer) {
memmove(_consoleBuffer, _consoleBuffer + bufferShift, CONSOLE_BUFFER_SIZE - bufferShift);
_consoleBufferPointer -= bufferShift;
}
strcpy(_consoleBufferPointer, src);
_consoleBufferPointer += charactersToWrite;
console_update_scroll();
}
void console_writeline(const char *src)
{
console_write(src);
console_write("\n");
}
static void console_update_scroll()
{
int lines = 0;
int maxLines = ((_consoleBottom - _consoleTop) / 10) - 1;
char *ch = strchr(_consoleBuffer, 0);
while (ch > _consoleBuffer && lines < maxLines) {
ch--;
if (*ch == '\n')
lines++;
}
if (*ch == '\n')
ch++;
_consoleViewBufferStart = ch;
}
void console_clear()
{
_consoleBuffer[0] = 0;
_consoleBufferPointer = _consoleBuffer;
}
static void console_clear_input()
{
_consoleCurrentLine[0] = 0;
gTextInputCursorPosition = 0;
gTextInputLength = 0;
}
static void console_history_add(const char *src)
{
if (_consoleHistoryCount >= CONSOLE_HISTORY_SIZE) {
for (int i = 0; i < _consoleHistoryCount - 1; i++)
memcpy(_consoleHistory[i], _consoleHistory[i + 1], CONSOLE_INPUT_SIZE);
_consoleHistoryCount--;
}
memcpy(_consoleHistory[_consoleHistoryCount++], src, CONSOLE_INPUT_SIZE);
_consoleHistoryIndex = _consoleHistoryCount;
}
static int cc_clear(const char **argv, int argc)
{
console_clear();
return 0;
}
static int cc_echo(const char **argv, int argc)
{
if (argc > 0)
console_writeline(argv[0]);
return 0;
}
static int cc_help(const char **argv, int argc)
{
console_write_all_commands();
return 0;
}
typedef int (*console_command_func)(const char **argv, int argc);
typedef struct {
char *command;
console_command_func func;
} console_command;
console_command console_command_table[] = {
{ "clear", cc_clear },
{ "echo", cc_echo },
{ "help", cc_help }
};
static void console_write_all_commands()
{
for (int i = 0; i < countof(console_command_table); i++)
console_writeline(console_command_table[i].command);
}
void console_execute(const char *src)
{
console_writeline(_consoleCurrentLine);
int argc = 0;
int argvCapacity = 8;
char **argv = malloc(argvCapacity * sizeof(char*));
const char *start = src;
const char *end;
bool inQuotes = false;
do {
while (*start == ' ')
start++;
if (*start == '"') {
inQuotes = true;
start++;
} else {
inQuotes = false;
}
end = start;
while (*end != 0) {
if (*end == ' ' && !inQuotes)
break;
if (*end == '"' && inQuotes)
break;
end++;
}
int length = end - start;
if (length > 0) {
char *arg = malloc(length + 1);
memcpy(arg, start, length);
arg[length] = 0;
if (argc >= argvCapacity) {
argvCapacity *= 2;
argv = realloc(argv, argvCapacity * sizeof(char*));
}
argv[argc] = arg;
argc++;
}
start = end;
} while (*end != 0);
if (argc == 0)
return;
bool validCommand = false;
for (int i = 0; i < countof(console_command_table); i++) {
if (strcmp(argv[0], console_command_table[i].command) == 0) {
console_command_table[i].func(argv + 1, argc - 1);
validCommand = true;
break;
}
}
for (int i = 0; i < argc; i++)
free(argv[i]);
free(argv);
if (!validCommand) {
char output[] = { FORMAT_RED, "Unknown command. Type help to list available commands." };
console_writeline(output);
}
}

21
src/interface/console.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef _CONSOLE_H_
#define _CONSOLE_H_
#include "../common.h"
extern bool gConsoleOpen;
void console_open();
void console_close();
void console_toggle();
void console_update();
void console_draw(rct_drawpixelinfo *dpi);
void console_input(int c);
void console_write(const char *src);
void console_writeline(const char *src);
void console_execute(const char *src);
void console_clear();
#endif

View File

@@ -638,6 +638,11 @@ void format_string(char *dest, rct_string_id format, void *args)
format_string_part(&dest, format, (char**)&args);
}
void format_string_raw(char *dest, char *src, void *args)
{
format_string_part_from_raw(&dest, src, &args);
}
/**
* rct2: 0x006E37F7
* error (eax)

View File

@@ -27,7 +27,7 @@
#include "string_ids.h"
void format_string(char *dest, rct_string_id format, void *args);
void format_string_part_from_raw(char **dest, const char *src, char **args);
void format_string_raw(char *dest, char *src, void *args);
void generate_string_file();
void error_string_quit(int error, rct_string_id format);
int get_string_length(char* buffer);

View File

@@ -61,6 +61,7 @@ extern const unsigned char *gKeysState;
extern unsigned char *gKeysPressed;
extern unsigned int gLastKeyPressed;
extern int gTextInputCursorPosition;
extern int gTextInputLength;
extern int gResolutionsAllowAnyAspectRatio;
extern int gNumResolutions;

View File

@@ -460,8 +460,10 @@ void platform_process_messages()
memmove(gTextInput + gTextInputCursorPosition + 1, gTextInput + gTextInputCursorPosition, gTextInputMaxLength - gTextInputCursorPosition - 1);
gTextInput[gTextInputCursorPosition] = new_char;
gTextInputLength++;
} else {
gTextInput[gTextInputLength++] = new_char;
gTextInput[gTextInputLength++] = 0;
}
else gTextInput[gTextInputLength++] = new_char;
gTextInputCursorPosition++;
}

View File

@@ -28,6 +28,7 @@
#include "drawing/drawing.h"
#include "editor.h"
#include "game.h"
#include "interface/console.h"
#include "interface/viewport.h"
#include "intro.h"
#include "localisation/date.h"
@@ -72,7 +73,7 @@ int rct2_init()
RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) = 0;
RCT2_GLOBAL(0x009AC310, char*) = RCT2_GLOBAL(RCT2_ADDRESS_CMDLINE, char*);
get_system_time();
srand(time(0));
srand((unsigned int)time(0));
RCT2_GLOBAL(0x009DEA69, short) = RCT2_GLOBAL(RCT2_ADDRESS_OS_TIME_DAY, short);
RCT2_GLOBAL(0x009DEA6B, short) = RCT2_GLOBAL(RCT2_ADDRESS_OS_TIME_MONTH, short);
if (!rct2_init_directories())
@@ -346,6 +347,9 @@ void rct2_update_2()
title_update();
else
game_update();
console_update();
console_draw(RCT2_ADDRESS(RCT2_ADDRESS_SCREEN_DPI, rct_drawpixelinfo));
}
void rct2_endupdate()

View File

@@ -649,9 +649,7 @@ static void window_top_toolbar_paint()
(gGameSpeed >= 5 ? FORMAT_YELLOW : FORMAT_GREEN),
175, (gGameSpeed >= 2 ? 175 : '\0'), (gGameSpeed >= 3 ? 175 : '\0'), (gGameSpeed >= 4 ? 175 : '\0'), '\0'
};
char* buffer;
buffer = RCT2_ADDRESS(RCT2_ADDRESS_COMMON_STRING_FORMAT_BUFFER, char);
format_string_part_from_raw(&buffer, &speedStr, NULL);
format_string_raw(RCT2_ADDRESS(RCT2_ADDRESS_COMMON_STRING_FORMAT_BUFFER, char), speedStr, NULL);
gfx_draw_string(dpi, speedStr, 0, x + 5, y + 14);
}