8 Commits

4 changed files with 61 additions and 35 deletions

35
lua.cpp
View File

@@ -140,10 +140,20 @@ extern "C" {
// map // map
// =============================================== // ===============================================
static int cpp_map_new(lua_State *L) {
int w = luaL_checknumber(L, 1);
int h = luaL_checknumber(L, 2);
uint8_t s = newsurf(w, h);
uint8_t old = getmap(); if (old) freesurf(old);
setmap(s);
return 0;
}
static int cpp_map_load(lua_State *L) { static int cpp_map_load(lua_State *L) {
const char* str = luaL_checkstring(L, 1); const char* str = luaL_checkstring(L, 1);
setmap(loadsurf(str)); uint8_t s = loadsurf(str);
uint8_t old = getmap(); if (old) freesurf(old);
setmap(s);
return 0; return 0;
} }
@@ -168,6 +178,8 @@ extern "C" {
uint8_t celw = luaL_checknumber(L, 5); uint8_t celw = luaL_checknumber(L, 5);
uint8_t celh = luaL_checknumber(L, 6); uint8_t celh = luaL_checknumber(L, 6);
uint8_t layer = luaL_optinteger(L, 7, 0);*/ uint8_t layer = luaL_optinteger(L, 7, 0);*/
uint8_t surface = luaL_checkinteger(L, 1);
setsource(surface);
map(); //celx, cely, sx, sy, celw, celh, layer); map(); //celx, cely, sx, sy, celw, celh, layer);
return 0; return 0;
} }
@@ -395,22 +407,22 @@ extern "C" {
} }
static int cpp_draw_rect(lua_State *L) { static int cpp_draw_rect(lua_State *L) {
int x0 = luaL_checknumber(L, 1); int x = luaL_checknumber(L, 1);
int y0 = luaL_checknumber(L, 2); int y = luaL_checknumber(L, 2);
int x1 = luaL_checknumber(L, 3); int w = luaL_checknumber(L, 3);
int y1 = luaL_checknumber(L, 4); int h = luaL_checknumber(L, 4);
uint8_t color = luaL_checkinteger(L, 5); uint8_t color = luaL_checkinteger(L, 5);
rect(x0, y0, x1, y1, color); rect(x, y, w, h, color);
return 0; return 0;
} }
static int cpp_draw_rectfill(lua_State *L) { static int cpp_draw_rectfill(lua_State *L) {
int x0 = luaL_checknumber(L, 1); int x = luaL_checknumber(L, 1);
int y0 = luaL_checknumber(L, 2); int y = luaL_checknumber(L, 2);
int x1 = luaL_checknumber(L, 3); int w = luaL_checknumber(L, 3);
int y1 = luaL_checknumber(L, 4); int h = luaL_checknumber(L, 4);
uint8_t color = luaL_checkinteger(L, 5); uint8_t color = luaL_checkinteger(L, 5);
rectfill(x0, y0, x1, y1, color); rectfill(x, y, w, h, color);
return 0; return 0;
} }
@@ -833,6 +845,7 @@ void push_lua_funcs() {
lua_setglobal(L, "surface"); lua_setglobal(L, "surface");
lua_newtable(L); lua_newtable(L);
lua_pushcfunction(L,cpp_map_new); lua_setfield(L, -2, "new");
lua_pushcfunction(L,cpp_map_load); lua_setfield(L, -2, "load"); lua_pushcfunction(L,cpp_map_load); lua_setfield(L, -2, "load");
lua_pushcfunction(L,cpp_map_save); lua_setfield(L, -2, "save"); lua_pushcfunction(L,cpp_map_save); lua_setfield(L, -2, "save");
lua_pushcfunction(L,cpp_map_set); lua_setfield(L, -2, "set"); lua_pushcfunction(L,cpp_map_set); lua_setfield(L, -2, "set");

View File

@@ -203,7 +203,7 @@ uint8_t newsurf(int w, int h) {
surfaces[i].w = w; surfaces[i].w = w;
surfaces[i].h = h; surfaces[i].h = h;
surfaces[i].size = w*h; surfaces[i].size = w*h;
surfaces[i].p = (uint8_t*)malloc(surfaces[i].size); surfaces[i].p = (uint8_t*)calloc(surfaces[i].size,1);
return i; return i;
} }
@@ -499,12 +499,13 @@ void simple_pset(int x, int y, uint8_t color) {
void cls(uint8_t color) { void cls(uint8_t color) {
const uint8_t col = ds::draw_palette[color]; const uint8_t col = ds::draw_palette[color];
for (int y=ds::clip[1]; y<=ds::clip[3];++y) { /*for (int y=ds::clip[1]; y<=ds::clip[3];++y) {
for (int x=ds::clip[0]; x<=ds::clip[2];++x) { for (int x=ds::clip[0]; x<=ds::clip[2];++x) {
simple_pset(x,y,col); simple_pset(x,y,col);
} }
} }*/
//SDL_memset(dest_surface->p, color, dest_surface->size);
SDL_memset(dest_surface->p, col, dest_surface->size);
} }
void color(uint8_t color) { void color(uint8_t color) {
@@ -675,25 +676,29 @@ void vline(int x, int y0, int y1, uint8_t color) {
vline(x, y0, y1); vline(x, y0, y1);
} }
void rect(int x0, int y0, int x1, int y1) { void rect(int x, int y, int w, int h) {
hline(x0, y0, x1); int x1 = w+x-1;
hline(x0, y1, x1); int y1 = h+y-1;
vline(x0, y0, y1); hline(x, y, x1);
vline(x1, y0, y1); hline(x, y1, x1);
vline(x, y, y1);
vline(x1, y, y1);
} }
void rect(int x0, int y0, int x1, int y1, uint8_t color) { void rect(int x, int y, int w, int h, uint8_t color) {
ds::pen_color = color; ds::pen_color = color;
rect(x0, y0, x1, y1); rect(x, y, w, h);
} }
void rectfill(int x0, int y0, int x1, int y1) { void rectfill(int x, int y, int w, int h) {
for (int y=y0; y<=y1; ++y) hline(x0, y, x1); int x1 = w+x-1;
int y1 = h+y-1;
for (int i=y; i<=y1; ++i) hline(x, i, x1);
} }
void rectfill(int x0, int y0, int x1, int y1, uint8_t color) { void rectfill(int x, int y, int w, int h, uint8_t color) {
ds::pen_color = color; ds::pen_color = color;
rectfill(x0, y0, x1, y1); rectfill(x, y, w, h);
} }
void fillp(uint16_t pat, bool transparent) { void fillp(uint16_t pat, bool transparent) {
@@ -1022,19 +1027,21 @@ void tvline(int x, int y0, int y1, float mx, float my, float mdx, float mdy) {
} }
uint8_t mget(int celx, int cely) { uint8_t mget(int celx, int cely) {
if (!map_surface) return 0;
if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return 0; if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return 0;
return TILES(celx, cely); return TILES(celx, cely);
} }
void mset(int celx, int cely, uint8_t snum) { void mset(int celx, int cely, uint8_t snum) {
if (!map_surface) return;
if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return; if (celx < 0 || celx > (map_surface->w-1) || cely < 0 || cely > (map_surface->h-1)) return;
TILES(celx, cely) = snum; TILES(celx, cely) = snum;
} }
void map() { //int celx, int cely, int sx, int sy, uint8_t celw, uint8_t celh, uint8_t layer) { void map() { //int celx, int cely, int sx, int sy, uint8_t celw, uint8_t celh, uint8_t layer) {
if (map_surface==NULL) return; if (map_surface==NULL) return;
uint8_t celw = map_surface->w >> 3; uint8_t celw = map_surface->w;// >> 3;
uint8_t celh = map_surface->h >> 3; uint8_t celh = map_surface->h;// >> 3;
int celx = 0; int celx = 0;
int cely = 0; int cely = 0;
//if (celw <= 0 || celh <= 0 || celw >= TILES_WIDTH || celh >= TILES_HEIGHT) return; //if (celw <= 0 || celh <= 0 || celw >= TILES_WIDTH || celh >= TILES_HEIGHT) return;

8
mini.h
View File

@@ -156,11 +156,11 @@ void hline(int x0, int y, int x1, uint8_t color);
void vline(int x, int y0, int y1); void vline(int x, int y0, int y1);
void vline(int x, int y0, int y1, uint8_t color); void vline(int x, int y0, int y1, uint8_t color);
void rect(int x0, int y0, int x1, int y1); void rect(int x, int y, int w, int h);
void rect(int x0, int y0, int x1, int y1, uint8_t color); void rect(int x, int y, int w, int h, uint8_t color);
void rectfill(int x0, int y0, int x1, int y1); void rectfill(int x, int y, int w, int h);
void rectfill(int x0, int y0, int x1, int y1, uint8_t color); void rectfill(int x, int y, int w, int h, uint8_t color);
void fillp(uint16_t pat, bool transparent = false); void fillp(uint16_t pat, bool transparent = false);

View File

@@ -69,6 +69,11 @@ function surface.getPixel(x, y) end
---@class tilemap ---@class tilemap
tilemap = {} tilemap = {}
---@param w number
---@param h number
---Create new map specifying width and height
function tilemap.new(w, h) end
---@param filename string ---@param filename string
---@return number surface ---@return number surface
---Load a tilemap from a file and set it as current tilemap ---Load a tilemap from a file and set it as current tilemap
@@ -82,8 +87,9 @@ function tilemap.save(filename) end
---Set surface as the current tilemap ---Set surface as the current tilemap
function tilemap.set(surface) end function tilemap.set(surface) end
---Draw the tilemap ---@param surface number
function tilemap.draw() end ---Draw the tilemap, using the provided surface as tile graphics source
function tilemap.draw(surface) end
---@param x number ---@param x number
---@param y number ---@param y number
@@ -403,7 +409,7 @@ function window.setResolution(w, h) end
config = {} config = {}
---@param key string ---@param key string
---@param value string ---@param value any
---Sets the value of a key in the configuration file ---Sets the value of a key in the configuration file
function config.setKey(key, value) end function config.setKey(key, value) end