1
0

VSYNC,CLS実装,引数なし関数をコンパイルするときに暴走しないように

This commit is contained in:
otya128
2015-07-12 11:48:23 +09:00
parent 7ad5c9ff3c
commit f985fbbede
4 changed files with 241 additions and 61 deletions

View File

@@ -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;
}
}

View File

@@ -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()
@@ -176,6 +186,13 @@ template GetFunctionParamType(T, string N)
{
enum GetFunctionParamType = mixin("[" ~ Array!(ParameterTypeTuple!(__traits(getMember, T, N))) ~ "]");
private template Array(P...)
{
static if(P.length == 0)
{
const string arg = "";
enum Array = "";
}
else
{
static if(is(P[0] == double))
{
@@ -194,9 +211,16 @@ template GetFunctionParamType(T, string N)
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 ~ ")";
@@ -207,6 +231,7 @@ template GetFunctionParamType(T, string N)
}
}
}
}
template AddFuncArg(int L, int N, int M, P...)
{
enum I = L - N;

View File

@@ -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));

View File

@@ -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;
void run()
int vsyncFrame;
int vsyncCount;
void vsync(int f)
{
init();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SMILEBASIC", SDL_WINDOWPOS_UNDEFINED,
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);
/+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)