Skip to content

Commit

Permalink
add draw image
Browse files Browse the repository at this point in the history
  • Loading branch information
swwind committed Oct 22, 2020
1 parent de1c76a commit cf8c9dd
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 72 deletions.
50 changes: 41 additions & 9 deletions aurora.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,68 @@ Napi::Value BindWindowEventCallback(const Napi::CallbackInfo& info) {
Napi::Value DrawLine(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object config = info[0].As<Napi::Object>();
KColor color = parseColor(&env, config.Get("color"));
KPoint st = parsePoint(&env, config.Get("start"));
KPoint ed = parsePoint(&env, config.Get("end"));
KColor* color = parseColor(config.Get("color"));
KPoint* st = parsePoint(config.Get("start"));
KPoint* ed = parsePoint(config.Get("end"));

Render::SetColor(color);
Render::DrawLine(st, ed);

delete color;
delete st;
delete ed;
return env.Undefined();
}
Napi::Value DrawPoint(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object config = info[0].As<Napi::Object>();
KColor color = parseColor(&env, config.Get("color"));
KPoint point = parsePoint(&env, config.Get("point"));
KColor* color = parseColor(config.Get("color"));
KPoint* point = parsePoint(config.Get("point"));

Render::SetColor(color);
Render::DrawPoint(point);

delete color;
delete point;
return env.Undefined();
}
Napi::Value DrawRect(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object config = info[0].As<Napi::Object>();
KColor color = parseColor(&env, config.Get("color"));
KRect rect = parseRect(&env, config.Get("rect"));
KColor* color = parseColor(config.Get("color"));
KRect* rect = parseRect(config.Get("rect"));

Render::SetColor(color);
Render::DrawRect(rect);

delete color;
delete rect;
return env.Undefined();
}
Napi::Value FillRect(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object config = info[0].As<Napi::Object>();
KColor color = parseColor(&env, config.Get("color"));
KRect rect = parseRect(&env, config.Get("rect"));
KColor* color = parseColor(config.Get("color"));
KRect* rect = parseRect(config.Get("rect"));

Render::SetColor(color);
Render::FillRect(rect);

delete color;
delete rect;
return env.Undefined();
}
Napi::Value DrawImage(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Object config = info[0].As<Napi::Object>();
int texture = config.Get("texture").As<Napi::Number>().Int32Value();
KRect* srcrect = parseRect(config.Get("srcrect"));
KRect* dstrect = parseRect(config.Get("dstrect"));

Render::DrawImage(texture, srcrect, dstrect);

delete srcrect;
delete dstrect;
return env.Undefined();
}

Expand Down Expand Up @@ -123,6 +145,12 @@ Napi::Value RenderClose(const Napi::CallbackInfo& info) {
Render::close();
return info.Env().Undefined();
}
Napi::Value RegisterTexture(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string src = info[0].As<Napi::String>();
int tid = Render::registerTexture(src);
return Napi::Number::New(env, tid);
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["startEventLoop"] = Napi::Function::New(env, StartEventLoop);
Expand All @@ -135,12 +163,16 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["drawPoint"] = Napi::Function::New(env, DrawPoint);
exports["drawRect"] = Napi::Function::New(env, DrawRect);
exports["fillRect"] = Napi::Function::New(env, FillRect);
exports["drawImage"] = Napi::Function::New(env, DrawImage);

// render
exports["init"] = Napi::Function::New(env, RenderInit);
exports["quit"] = Napi::Function::New(env, RenderQuit);
exports["render"] = Napi::Function::New(env, RenderPresent);
exports["close"] = Napi::Function::New(env, RenderClose);

// sources
exports["registerTexture"] = Napi::Function::New(env, RegisterTexture);
return exports;
}

Expand Down
9 changes: 9 additions & 0 deletions aurora.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ type KDrawRectConfig = {
rect: KRect;
}

type KDrawImageConfig = {
texture: number;
srcrect?: KRect;
dstrect?: KRect;
}

type KWindowConfig = {
title: string;
w: number;
Expand Down Expand Up @@ -109,12 +115,15 @@ export function drawLine(config: KDrawLineConfig): void;
export function drawPoint(config: KDrawPointConfig): void;
export function drawRect(config: KDrawRectConfig): void;
export function fillRect(config: KDrawRectConfig): void;
export function drawImage(config: KDrawImageConfig): void;

export function init(config: KWindowConfig): boolean;
export function quit(): void;
export function render(): void;
export function close(): void;

export function registerTexture(path: string): number;

export const color: {
rgb(r: number, g: number, b: number): KColor;
rgba(r: number, g: number, b: number, a: number): KColor;
Expand Down
Binary file added example/huaji.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 7 additions & 19 deletions example/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,19 @@ if (!aurora.init({
});
}

const huaji = aurora.registerTexture('example/huaji.png');

setInterval(() => {
const time = Date.now();
if (keys["w"]) {
y -= 0.5 * (time - lasttime);
}
if (keys["s"]) {
y += 0.5 * (time - lasttime);
}
if (keys["a"]) {
x -= 0.5 * (time - lasttime);
}
if (keys["d"]) {
x += 0.5 * (time - lasttime);
}
if (keys["w"] || keys["UP"]) { y -= 0.5 * (time - lasttime); }
if (keys["s"] || keys["DOWN"]) { y += 0.5 * (time - lasttime); }
if (keys["a"] || keys["LEFT"]) { x -= 0.5 * (time - lasttime); }
if (keys["d"] || keys["RIGHT"]) { x += 0.5 * (time - lasttime); }
lasttime = time;
const c1 = (Math.sin(time/5000) + 1) * 128;
const c2 = (Math.sin(time/4000) + 1) * 128;
const c3 = (Math.sin(time/3000) + 1) * 128;
aurora.fillRect({
color: aurora.color.white,
rect: { x: 0, y: 0, w: 1280, h: 720 },
});
aurora.fillRect({
color: hover ? aurora.color.blue : aurora.color.rgb(c1, c2, c3),
rect: { x, y, w, h }
});
aurora.drawImage({ texture: huaji, dstrect: { x, y, w, h } });
aurora.render();
}, 1000 / 60);
37 changes: 19 additions & 18 deletions lib/render.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "render.h"

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;

Expand Down Expand Up @@ -77,7 +73,7 @@ void RenderCallbacks::registerMouseEventCallback(Napi::Env env, const Napi::Func
}
void RenderCallbacks::registerWindowEventCallback(Napi::Env env, const Napi::Function& fn) {
windowEventCallbackList.push_back(Napi::ThreadSafeFunction::New(
env, fn, "Ireina saikou!!", 0, 1,
env, fn, "Kotori saikou!!", 0, 1,
[] (Napi::Env) { }
));
}
Expand All @@ -93,23 +89,30 @@ SDL_Texture* loadTexture(std::string path) {
return newTexture;
}

void Render::SetColor(const KColor& color) {
SDL_SetRenderDrawColor(gRenderer, color.r, color.g, color.b, color.a);
void Render::SetColor(const KColor* color) {
SDL_SetRenderDrawColor(gRenderer, color->r, color->g, color->b, color->a);
}

void Render::DrawLine(const KPoint& st, const KPoint& ed) {
SDL_RenderDrawLine(gRenderer, st.x, st.y, ed.x, ed.y);
void Render::DrawLine(const KPoint* st, const KPoint* ed) {
SDL_RenderDrawLine(gRenderer, st->x, st->y, ed->x, ed->y);
}

void Render::DrawPoint(const KPoint& p) {
SDL_RenderDrawPoint(gRenderer, p.x, p.y);
void Render::DrawPoint(const KPoint* p) {
SDL_RenderDrawPoint(gRenderer, p->x, p->y);
}

void Render::DrawRect(const KRect& r) {
SDL_RenderDrawRect(gRenderer, &r);
void Render::DrawRect(const KRect* r) {
SDL_RenderDrawRect(gRenderer, r);
}
void Render::FillRect(const KRect& r) {
SDL_RenderFillRect(gRenderer, &r);
void Render::FillRect(const KRect* r) {
SDL_RenderFillRect(gRenderer, r);
}
void Render::DrawImage(const int& tid, const KRect* srcrect, const KRect* dstrect) {
auto res = textureMap.find(tid);
if (res == textureMap.end()) {
return;
}
SDL_RenderCopy(gRenderer, res -> second, srcrect, dstrect);
}

void Render::RenderPresent() {
Expand All @@ -129,7 +132,6 @@ int Render::registerTexture(std::string src) {

bool Render::init(const char *title, int x, int y, int w, int h, Uint32 flags) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return false;
}

Expand All @@ -143,7 +145,7 @@ bool Render::init(const char *title, int x, int y, int w, int h, Uint32 flags) {
return false;
}

const int imgflag = IMG_INIT_PNG;
const int imgflag = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP;
if((IMG_Init(imgflag) & imgflag) != imgflag) {
return false;
}
Expand All @@ -167,7 +169,6 @@ void Render::close() {
gWindow = NULL;
gRenderer = NULL;

//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
Expand Down
11 changes: 6 additions & 5 deletions lib/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ SDL_Texture* loadTexture(std::string path);

namespace Render {

void SetColor(const KColor& color);
void DrawLine(const KPoint& st, const KPoint& ed);
void DrawPoint(const KPoint& p);
void DrawRect(const KRect& r);
void FillRect(const KRect& r);
void SetColor(const KColor* color);
void DrawLine(const KPoint* st, const KPoint* ed);
void DrawPoint(const KPoint* p);
void DrawRect(const KRect* r);
void FillRect(const KRect* r);
void DrawImage(const int& tid, const KRect* srcrect, const KRect* dstrect);
void RenderPresent();
int registerTexture(std::string src);
bool init(const char *title, int x, int y, int w, int h, Uint32 flags);
Expand Down
30 changes: 12 additions & 18 deletions lib/shape.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
#include "shape.h"

KPoint parsePoint(Napi::Env* env, Napi::Value point) {
KPoint* parsePoint(const Napi::Value& point) {
if (!point.IsObject()) {
Napi::TypeError::New(*env, "Invalid point format")
.ThrowAsJavaScriptException();
return NULL;
}

Napi::Object obj = point.As<Napi::Object>();
Napi::Value xx = obj.Get("x");
Napi::Value yy = obj.Get("y");
if (!xx.IsNumber() || !yy.IsNumber()) {
Napi::TypeError::New(*env, "Invalid point format")
.ThrowAsJavaScriptException();
return NULL;
}

int x = xx.As<Napi::Number>().Int32Value();
int y = yy.As<Napi::Number>().Int32Value();

return (KPoint) { x, y };
return new KPoint({ x, y });
}

KRect parseRect(Napi::Env* env, Napi::Value rect) {
KRect* parseRect(const Napi::Value& rect) {
if (!rect.IsObject()) {
Napi::TypeError::New(*env, "Invalid rect format")
.ThrowAsJavaScriptException();
return NULL;
}

Napi::Object obj = rect.As<Napi::Object>();
Expand All @@ -32,31 +29,28 @@ KRect parseRect(Napi::Env* env, Napi::Value rect) {
Napi::Value ww = obj.Get("w");
Napi::Value hh = obj.Get("h");
if (!xx.IsNumber() || !yy.IsNumber() || !ww.IsNumber() || !hh.IsNumber()) {
Napi::TypeError::New(*env, "Invalid rect format")
.ThrowAsJavaScriptException();
return NULL;
}

int x = xx.As<Napi::Number>().Int32Value();
int y = yy.As<Napi::Number>().Int32Value();
int w = ww.As<Napi::Number>().Int32Value();
int h = hh.As<Napi::Number>().Int32Value();

return { x, y, w, h };
return new KRect({ x, y, w, h });
}

KColor parseColor(Napi::Env* env, Napi::Value color) {
KColor* parseColor(const Napi::Value& color) {
if (!color.IsObject()) {
Napi::TypeError::New(*env, "Invalid color format")
.ThrowAsJavaScriptException();
return NULL;
}
Napi::Object obj = color.As<Napi::Object>();
Napi::Value rr = obj.Get("r");
Napi::Value gg = obj.Get("g");
Napi::Value bb = obj.Get("b");
Napi::Value aa = obj.Get("a");
if (!rr.IsNumber() || !gg.IsNumber() || !bb.IsNumber() || !aa.IsNumber()) {
Napi::TypeError::New(*env, "Invalid color format")
.ThrowAsJavaScriptException();
return NULL;
}

// FIXME: maybe dangerous here
Expand All @@ -65,5 +59,5 @@ KColor parseColor(Napi::Env* env, Napi::Value color) {
uint8_t b = bb.As<Napi::Number>().Uint32Value();
uint8_t a = aa.As<Napi::Number>().Uint32Value();

return (KColor) { r, g, b, a };
return new KColor({ r, g, b, a });
}
6 changes: 3 additions & 3 deletions lib/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct KPoint { int x, y; };
struct KColor { uint8_t r, g, b, a; };
typedef SDL_Rect KRect;

KPoint parsePoint(Napi::Env* env, Napi::Value point);
KRect parseRect(Napi::Env* env, Napi::Value rect);
KColor parseColor(Napi::Env* env, Napi::Value color);
KPoint* parsePoint(const Napi::Value& point);
KRect* parseRect(const Napi::Value& rect);
KColor* parseColor(const Napi::Value& color);

#endif

0 comments on commit cf8c9dd

Please sign in to comment.