From f985fbbede28c13f0e25b8fe7e15369a98a137cc Mon Sep 17 00:00:00 2001 From: otya128 Date: Sun, 12 Jul 2015 11:48:23 +0900 Subject: [PATCH] =?UTF-8?q?VSYNC,CLS=E5=AE=9F=E8=A3=85,=E5=BC=95=E6=95=B0?= =?UTF-8?q?=E3=81=AA=E3=81=97=E9=96=A2=E6=95=B0=E3=82=92=E3=82=B3=E3=83=B3?= =?UTF-8?q?=E3=83=91=E3=82=A4=E3=83=AB=E3=81=99=E3=82=8B=E3=81=A8=E3=81=8D?= =?UTF-8?q?=E3=81=AB=E6=9A=B4=E8=B5=B0=E3=81=97=E3=81=AA=E3=81=84=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SMILEBASIC/VM.d | 12 +- SMILEBASIC/builtinfunctions.d | 73 ++++++++---- SMILEBASIC/compiler.d | 2 +- SMILEBASIC/petitcomputer.d | 215 ++++++++++++++++++++++++++++------ 4 files changed, 241 insertions(+), 61 deletions(-) diff --git a/SMILEBASIC/VM.d b/SMILEBASIC/VM.d index e50b525..8835b8c 100644 --- a/SMILEBASIC/VM.d +++ b/SMILEBASIC/VM.d @@ -70,6 +70,11 @@ class VM } void push(ref Value value) { + if(stacki >= this.stack.length) + { + writeln("Stack OF"); + readln(); + } stack[stacki++] = value; } void push(Value value) @@ -138,17 +143,17 @@ class PrintCode : Code switch(arg.type) { case ValueType.Integer: - write(arg.integerValue); + //write(arg.integerValue); if(vm.petitcomputer) vm.petitcomputer.printConsole(arg.integerValue); break; case ValueType.Double: - write(arg.doubleValue); + //write(arg.doubleValue); if(vm.petitcomputer) vm.petitcomputer.printConsole(arg.doubleValue); break; case ValueType.String: - write(arg.stringValue); + //write(arg.stringValue); if(vm.petitcomputer) vm.petitcomputer.printConsole(arg.stringValue); break; @@ -880,5 +885,6 @@ class CallBuiltinFunction : Code result = vm.stack[vm.stacki - argcount..vm.stacki - argcount + outcount];//雑; } func.func(vm.petitcomputer, arg, result); + vm.stacki -= func.argments.length; } } diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index 769c3f5..e0f8479 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -84,6 +84,16 @@ class BuiltinFunction p.consoleForeColor = cast(int)fore; p.consoleBackColor = cast(int)back; } + static void VSYNC(PetitComputer p, DefaultValue!int time) + { + time.setDefaultValue(1); + p.vsync(cast(int)time); + } + //TODO:プチコンのCLSには引数の個数制限がない + static void CLS(PetitComputer p/*vaarg*/) + { + p.cls; + } //alias void function(PetitComputer, Value[], Value[]) BuiltinFunc; static BuiltinFunction[wstring] builtinFunctions; static this() @@ -177,33 +187,48 @@ template GetFunctionParamType(T, string N) enum GetFunctionParamType = mixin("[" ~ Array!(ParameterTypeTuple!(__traits(getMember, T, N))) ~ "]"); private template Array(P...) { - static if(is(P[0] == double)) + static if(P.length == 0) { - const string arg = "ValueType.Double, false"; + const string arg = ""; + enum Array = ""; } - else static if(is(P[0] == int)) + else { - const string arg = "ValueType.Integer, false"; - } - else static if(is(P[0] == DefaultValue!int)) - { - const string arg = "ValueType.Integer, true"; - } - else static if(is(P[0] == DefaultValue!(int, false))) - { - const string arg = "ValueType.Integer, true"; - } - else static if(is(P[0] == PetitComputer)) - { - enum Array = Array!(P[1..$]); - } - static if(1 == P.length && !is(P[0] == PetitComputer)) - { - enum Array = "BuiltinFunctionArgument(" ~ arg ~ ")"; - } - else static if(!is(P[0] == PetitComputer)) - { - enum Array = "BuiltinFunctionArgument(" ~ arg ~ ")," ~ Array!(P[1..$]); + static if(is(P[0] == double)) + { + const string arg = "ValueType.Double, false"; + } + else static if(is(P[0] == int)) + { + const string arg = "ValueType.Integer, false"; + } + else static if(is(P[0] == DefaultValue!int)) + { + const string arg = "ValueType.Integer, true"; + } + else static if(is(P[0] == DefaultValue!(int, false))) + { + const string arg = "ValueType.Integer, true"; + } + else static if(is(P[0] == PetitComputer)) + { + static if(P.length != 0) + { + enum Array = Array!(P[1..$]); + } + else + { + enum Array = ""; + } + } + static if(1 == P.length && !is(P[0] == PetitComputer)) + { + enum Array = "BuiltinFunctionArgument(" ~ arg ~ ")"; + } + else static if(!is(P[0] == PetitComputer)) + { + enum Array = "BuiltinFunctionArgument(" ~ arg ~ ")," ~ Array!(P[1..$]); + } } } } diff --git a/SMILEBASIC/compiler.d b/SMILEBASIC/compiler.d index a8868cc..6323656 100644 --- a/SMILEBASIC/compiler.d +++ b/SMILEBASIC/compiler.d @@ -613,7 +613,7 @@ class Compiler auto bfun = otya.smilebasic.builtinfunctions.BuiltinFunction.builtinFunctions.get(func.name, null); if(bfun) { - auto k = bfun.argments.length - func.args.length; + int k = bfun.argments.length - func.args.length; foreach(l;0..k) { genCodeImm(Value(ValueType.Void)); diff --git a/SMILEBASIC/petitcomputer.d b/SMILEBASIC/petitcomputer.d index 03cc592..2ada447 100644 --- a/SMILEBASIC/petitcomputer.d +++ b/SMILEBASIC/petitcomputer.d @@ -63,6 +63,7 @@ class PetitComputer wchar charater; int foreColor; int backColor; + byte attr; } SDL_Color PetitColor(byte r, byte g, byte b, byte a) { @@ -71,6 +72,8 @@ class PetitComputer ConsoleCharacter[][] console; GraphicPage[] grp; GraphicPage GRPF; + GraphicPage[] GRPFColor; + GraphicPage[][] GRPFColorFore; //PNG画像から透過色のpixelを指定して透過しGRPを作る GraphicPage createGraphicPage(string file, int pixel) { @@ -89,6 +92,67 @@ class PetitComputer SDL_RWclose(stream); return grp; } + GraphicPage createGRPF(int color, SDL_Surface *src) + { + SDL_Surface* surface = SDL_CreateRGBSurface(0, src.w, src.h, 32, 0, 0, 0, 0); + auto srcpixels = (cast(uint*)src.pixels); + auto pixels = (cast(uint*)surface.pixels); + for(int x = 0; x < src.w; x++) + { + for(int y = 0; y < src.h; y++) + { + ubyte r, g, b, a; + SDL_GetRGBA(*srcpixels, src.format, &r, &g, &b, &a); + //if(a == 0) + { + auto back = consoleColor[color]; + r = back >> 16 & 0xFF; + g = back >> 8 & 0xFF; + b = back & 0xFF; + a = back >> 24 & 0xFF; + } + *pixels = SDL_MapRGBA(surface.format, r, g, b, a); + pixels++; + srcpixels++; + } + } + return new GraphicPage(surface); + } + GraphicPage createGRPF(int color, int colorFore, SDL_Surface *src) + { + SDL_Surface* surface = SDL_CreateRGBSurface(0, src.w, src.h, 32, 0, 0, 0, 0); + auto srcpixels = (cast(uint*)src.pixels); + auto pixels = (cast(uint*)surface.pixels); + for(int x = 0; x < src.w; x++) + { + for(int y = 0; y < src.h; y++) + { + ubyte r, g, b, a; + SDL_GetRGBA(*srcpixels, src.format, &r, &g, &b, &a); + if(r == 158 && g == 0 && b == 93 && a == 255) + { + auto back = consoleColor[color]; + r = back >> 16 & 0xFF; + g = back >> 8 & 0xFF; + b = back & 0xFF; + a = back >> 24 & 0xFF; + } + else + { + //雑 + auto fore = consoleColor[colorFore]; + r = fore >> 16 & 0xFF; + g = fore >> 8 & 0xFF; + b = fore & 0xFF; + a = fore >> 24 & 0xFF; + } + *pixels = SDL_MapRGBA(surface.format, r, g, b, a); + pixels++; + srcpixels++; + } + } + return new GraphicPage(surface); + } struct Point { int x, y; @@ -158,6 +222,20 @@ class PetitComputer fontFile); } GRPF = createGraphicPage(fontFile, 0); + GRPFColor = new GraphicPage[consoleColor.length]; + GRPFColorFore = new GraphicPage[][consoleColor.length]; + for(int i = 0; i < GRPFColor.length; i++) + { + GRPFColor[i] = createGRPF(i, GRPF.surface); + } + /+for(int i = 0; i < GRPFColor.length; i++) + { + GRPFColorFore[i] = new GraphicPage[consoleColor.length]; + for(int j = 0; j < GRPFColor.length; j++) + { + GRPFColorFore[i][j] = createGRPF(i, j, GRPF.surface); + } + }+/ if(!exists(spriteFile)) { writeln("download sprite"); @@ -193,21 +271,80 @@ class PetitComputer { console[i] = new ConsoleCharacter[consoleWidth]; console[i][] = ConsoleCharacter(0, consoleForeColor, consoleBackColor); - /* - for(int j = 0; j < console[i].length; j++) - console[i][j] = '\0';*/ + } + } + void cls() + { + for(int i = 0; i < console.length; i++) + { + console[i][] = ConsoleCharacter(0, consoleForeColor, consoleBackColor); } } SDL_Renderer* renderer; + int vsyncFrame; + int vsyncCount; + void vsync(int f) + { + vsyncCount = 0; + vsyncFrame = f; + } + void render() + { + bool renderprofile; + try + { + window = SDL_CreateWindow("SMILEBASIC", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, 400, 240, + SDL_WINDOW_SHOWN); + renderer = SDL_CreateRenderer(window, -1, 0); + GRPF.createTexture(renderer); + for(int i = 0; i < GRPFColor.length; i++) + { + GRPFColor[i].createTexture(renderer); + } + write("OK!"); + SDL_Event event; + while(true) + { + auto profile = SDL_GetTicks(); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + renderConsole; + SDL_RenderPresent(renderer); + auto a = (SDL_GetTicks() - profile); + if(renderprofile) writeln(a); + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + SDL_DestroyWindow(window); + SDL_Quit(); + return; + + default: + break; + } + } + } + } + catch(Throwable t) + { + writeln(t); + } + } + SDL_Window* window; void run() { init(); SDL_Init(SDL_INIT_VIDEO); - SDL_Window* window = SDL_CreateWindow("SMILEBASIC", SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, 400, 240, - SDL_WINDOW_SHOWN); - renderer = SDL_CreateRenderer(window, -1, 0); - GRPF.createTexture(renderer); + /+for(int i = 0; i < GRPFColor.length; i++) + { + for(int j = 0; j < GRPFColor.length; j++) + { + GRPFColorFore[i][j].createTexture(renderer); + } + }+/ //とりあえず auto parser = new Parser(/*readText("FIZZBUZZ.TXT").to!wstring*//*" ?ABS(-1) @@ -219,7 +356,7 @@ NEXT DEF FACT(N) IF N<=1 THEN RETURN 1 RETURN N*FACT(N-1) -END"*/ +END"*//* `CLS : CL=0 : Z=0 WHILE 1 INC Z : IF Z>200 THEN Z=0 @@ -228,19 +365,31 @@ WHILE 1 PRINT "★ 梅雨で雨が多い季節ですね ★" NEXT CL=(CL+1) MOD 16 : VSYNC 2 -WEND`); +WEND`*/ +`CLS : CL=0 +@LOOP +FOR I=0 TO 15 + LOCATE 9,4+I : COLOR (CL+I) MOD 16 + PRINT "Ё プチコン3ゴウ Ж" +NEXT +CL=(CL+1) MOD 16 : VSYNC 1 +GOTO @LOOP` +); auto vm = parser.compile(); bool running = true; vm.init(this); + core.thread.Thread thread = new core.thread.Thread(&render); + thread.start(); + auto startTicks = SDL_GetTicks(); while (true) { - auto startTicks = SDL_GetTicks(); uint elapse; + startTicks = SDL_GetTicks(); do { try { - if(running) running = vm.runStep(); + if(!vsyncFrame && running) running = vm.runStep(); } catch(SmileBasicError sbe) { @@ -266,24 +415,8 @@ WEND`); } elapse = SDL_GetTicks() - startTicks; } while(elapse <= 1000 / 60); - SDL_Event event; - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_RenderClear(renderer); - renderConsole; - SDL_RenderPresent(renderer); - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: - SDL_DestroyWindow(window); - SDL_Quit(); - return; - - default: - break; - } - } + vsyncCount++; + if(vsyncFrame <= vsyncCount) vsyncFrame = 0; } SDL_DestroyWindow(window); SDL_Quit(); @@ -298,13 +431,29 @@ WEND`); for(int x = 0; x < consoleWidth; x++) { SDL_Rect rect = SDL_Rect(x * 8, y * 8, 8, 8); - auto back = consoleColor[console[y][x].backColor]; - SDL_SetRenderDrawColor(renderer, back >> 16 & 0xFF, back >> 8 & 0xFF, back & 0xFF, back >> 24 & 0xFF); - SDL_RenderFillRect(renderer, &rect); + auto back = console[y][x].backColor; auto fore = consoleColor[console[y][x].foreColor]; + auto texture = GRPFColor[back].texture; + //SDL_RenderCopy(renderer, texture, &fontTable[console[y][x].charater], &rect); SDL_SetTextureColorMod(GRPF.texture, fore >> 16 & 0xFF, fore >> 8 & 0xFF, fore & 0xFF); SDL_SetTextureAlphaMod(GRPF.texture, fore >> 24 & 0xFF); SDL_RenderCopy(renderer, GRPF.texture, &fontTable[console[y][x].charater], &rect); + /* + auto back = console[y][x].backColor; + auto fore = console[y][x].foreColor; + auto texture = GRPFColorFore[back][fore].texture; + SDL_RenderCopy(renderer, texture, &fontTable[console[y][x].charater], &rect);*/ + /* + auto back = consoleColor[console[y][x].backColor]; + auto texture = GRPFColor[back].texture; + SDL_RenderCopy(renderer, texture, &fontTable[console[y][x].charater], &rect); + texture = GRPF.texture; + //SDL_SetRenderDrawColor(renderer, back >> 16 & 0xFF, back >> 8 & 0xFF, back & 0xFF, back >> 24 & 0xFF); + //SDL_RenderFillRect(renderer, &rect); + auto fore = consoleColor[console[y][x].foreColor]; + SDL_SetTextureColorMod(texture, fore >> 16 & 0xFF, fore >> 8 & 0xFF, fore & 0xFF); + SDL_SetTextureAlphaMod(texture, fore >> 24 & 0xFF); + SDL_RenderCopy(renderer, texture, &fontTable[console[y][x].charater], &rect);*/ } } void printConsole(T...)(T args)