Skip to content

Commit

Permalink
This is really hard to get right
Browse files Browse the repository at this point in the history
  • Loading branch information
emmachase committed Sep 3, 2017
1 parent e056417 commit 6bc2c2b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/GPULib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ static int gpu_clear(lua_State *L) {
return 0;
}

static int gpu_set_fullscreen(lua_State *L) {
bool fsc = lua_toboolean(L, 1);
GPU_SetFullscreen(fsc, true);

return 0;
}

static int gpu_swap(lua_State *L) {
GPU_Clear(renderer);

Expand All @@ -231,6 +238,7 @@ static const luaL_Reg gpuLib[] = {
{ "drawPixel", gpu_draw_pixel },
{ "drawRectangle", gpu_draw_rectangle },
{ "blitPixels", gpu_blit_pixels },
{ "setFullscreen", gpu_set_fullscreen },
{ "clear", gpu_clear },
{ "swap", gpu_swap },
{NULL, NULL}
Expand Down
48 changes: 35 additions & 13 deletions src/fsLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ typedef struct {
FILE *fileStream;
bool open;
bool canWrite;
bool eof;
} fileHandleType;

static fileHandleType *checkFsObj(lua_State *L) {
Expand Down Expand Up @@ -237,9 +238,10 @@ static int fsOpenFile(lua_State *L) {
lua_setmetatable(L, -2);

*outObj = {
fileHandle_o,
fileHandle_o,
true,
mode[0] != 'r'
mode[0] != 'r',
false
};

return 1;
Expand Down Expand Up @@ -283,7 +285,13 @@ static int fsObjRead(lua_State *L) {
if (data->canWrite)
return luaL_error(L, "file is open for writing");

if (data->eof) {
lua_pushnil(L);
return 1;
}

if (feof(data->fileStream)) {
data->eof = true;
lua_pushnil(L);
return 1;
}
Expand All @@ -298,14 +306,19 @@ static int fsObjRead(lua_State *L) {

int st = ftell(data->fileStream);

fgets(dataBuf, FS_LINE_INCR, data->fileStream);
if (fgets(dataBuf, FS_LINE_INCR, data->fileStream) == NULL) {
lua_pushstring(L, "");
free(dataBuf);
return 1;
}

size_t dataBufLen = lineStrlen(dataBuf, FS_LINE_INCR);
if (dataBuf[dataBufLen - 1] == '\n' || dataBuf[dataBufLen - 1] == '\r') {
lua_pushlstring(L, dataBuf, dataBufLen - 1);
free(dataBuf);
return 1;
} else if (feof(data->fileStream)) {
data->eof = true;
dataBufLen = ftell(data->fileStream);
lua_pushlstring(L, dataBuf, dataBufLen - st);
free(dataBuf);
Expand All @@ -321,18 +334,19 @@ static int fsObjRead(lua_State *L) {

st = ftell(data->fileStream);

if (fgets(dataBuf + i * (FS_LINE_INCR - 1) * sizeof(char), FS_LINE_INCR, data->fileStream) == NULL) {
lua_pushnil(L);
free(dataBuf);
return 1;
}
if (fgets(dataBuf + i * (FS_LINE_INCR - 1) * sizeof(char), FS_LINE_INCR, data->fileStream) == NULL) {
lua_pushstring(L, "");
free(dataBuf);
return 1;
}

size_t dataBufLen = lineStrlen(dataBuf, bufLen / sizeof(char));
if (dataBuf[dataBufLen - 1] == '\n') {
lua_pushlstring(L, dataBuf, dataBufLen - 1);
free(dataBuf);
return 1;
} else if (feof(data->fileStream)) {
data->eof = true;
dataBufLen = ftell(data->fileStream);
lua_pushlstring(L, dataBuf, dataBufLen - st);
free(dataBuf);
Expand Down Expand Up @@ -364,6 +378,8 @@ static int fsObjRead(lua_State *L) {

lua_pushlstring(L, dataBuf, result);

data->eof = true;

free(dataBuf);

return 1;
Expand All @@ -376,14 +392,19 @@ static int fsObjRead(lua_State *L) {

int st = ftell(data->fileStream);

fgets(dataBuf, FS_LINE_INCR, data->fileStream);
if (fgets(dataBuf, FS_LINE_INCR, data->fileStream) == NULL) {
lua_pushstring(L, "");
free(dataBuf);
return 1;
}

size_t dataBufLen = lineStrlen(dataBuf, FS_LINE_INCR);
if (dataBuf[dataBufLen - 1] == '\n' || dataBuf[dataBufLen - 1] == '\r') {
lua_pushlstring(L, dataBuf, dataBufLen - 1);
free(dataBuf);
return 1;
} else if (feof(data->fileStream)) {
data->eof = true;
dataBufLen = ftell(data->fileStream);
lua_pushlstring(L, dataBuf, dataBufLen - st);
free(dataBuf);
Expand All @@ -400,17 +421,18 @@ static int fsObjRead(lua_State *L) {
st = ftell(data->fileStream);

if (fgets(dataBuf + i * (FS_LINE_INCR - 1) * sizeof(char), FS_LINE_INCR, data->fileStream) == NULL) {
lua_pushnil(L);
free(dataBuf);
return 1;
}
lua_pushstring(L, "");
free(dataBuf);
return 1;
}

size_t dataBufLen = lineStrlen(dataBuf, bufLen / sizeof(char));
if (dataBuf[dataBufLen - 1] == '\n' || dataBuf[dataBufLen - 1] == '\r') {
lua_pushlstring(L, dataBuf, dataBufLen - 1);
free(dataBuf);
return 1;
} else if (feof(data->fileStream)) {
data->eof = true;
dataBufLen = ftell(data->fileStream);
lua_pushlstring(L, dataBuf, dataBufLen - st);
free(dataBuf);
Expand Down
4 changes: 2 additions & 2 deletions src/rikoConsts.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#define OPEN_FS_DESC 128

#define SCRN_WIDTH 352
#define SCRN_HEIGHT 198
#define SCRN_WIDTH 320
#define SCRN_HEIGHT 180

#define sane_NUM_SCANCODES 512

Expand Down

0 comments on commit 6bc2c2b

Please sign in to comment.