1
0

約16倍高速化

This commit is contained in:
otya128
2015-08-18 22:33:15 +09:00
parent 4447035520
commit 7b72af7f91

View File

@@ -12,8 +12,8 @@ import core.sync.mutex;
import otya.smilebasic.parser; import otya.smilebasic.parser;
class GraphicPage class GraphicPage
{ {
static GLenum textureScaleMode = GL_NEAREST;
SDL_Surface* surface; SDL_Surface* surface;
SDL_Texture* texture;
this(SDL_Surface* s) this(SDL_Surface* s)
{ {
surface = s; surface = s;
@@ -21,7 +21,6 @@ class GraphicPage
GLuint glTexture; GLuint glTexture;
void createTexture(SDL_Renderer* renderer) void createTexture(SDL_Renderer* renderer)
{ {
texture = SDL_CreateTextureFromSurface(renderer, surface);
ubyte r,g,b,a; ubyte r,g,b,a;
SDL_GetRGBA(*cast(uint*)surface.pixels, surface.format, &r, &g, &b, &a); SDL_GetRGBA(*cast(uint*)surface.pixels, surface.format, &r, &g, &b, &a);
GLenum texture_format; GLenum texture_format;
@@ -48,8 +47,8 @@ class GraphicPage
// Bind the texture object // Bind the texture object
glBindTexture( GL_TEXTURE_2D, glTexture ); glBindTexture( GL_TEXTURE_2D, glTexture );
// Set the texture's stretching properties // Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, textureScaleMode );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, textureScaleMode );
// Edit the texture object's image data using the information SDL_Surface gives us // Edit the texture object's image data using the information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface.w, surface.h, 0, glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface.w, surface.h, 0,
@@ -93,6 +92,12 @@ class PetitComputer
0xFF7F7F7F, 0xFF7F7F7F,
0xFFFFFFFF, 0xFFFFFFFF,
]; ];
int[] consoleColorGL = new int[16];
uint toGLColor(uint color)
{
//ARGB -> ABGR
return (color & 0xFF00FF00) | (color >> 16 & 0xFF) | ((color & 0xFF) << 16);
}
struct ConsoleCharacter struct ConsoleCharacter
{ {
wchar charater; wchar charater;
@@ -301,6 +306,8 @@ class PetitComputer
DerelictSDL2.load(); DerelictSDL2.load();
DerelictSDL2Image.load(); DerelictSDL2Image.load();
DerelictGL.load(); DerelictGL.load();
for(int i = 0; i < consoleColor.length; i++)
consoleColorGL[i] = toGLColor(consoleColor[i]);
if(!exists(resourcePath)) if(!exists(resourcePath))
{ {
writeln("create ./resources"); writeln("create ./resources");
@@ -414,30 +421,29 @@ class PetitComputer
context = SDL_GL_CreateContext(window); context = SDL_GL_CreateContext(window);
GRPF.createTexture(renderer); GRPF.createTexture(renderer);
if (!context) return; if (!context) return;
//glViewport(0, 0, 400, 240); glViewport(0, 0, 400, 240);
glClearColor(0.0f, 1.0f, 0.0f, 0.0f); glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
while(true) while(true)
{ {
auto profile = SDL_GetTicks();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
GLenum aa = glGetError(); GLenum aa = glGetError();
glBindTexture( GL_TEXTURE_2D, GRPF.glTexture ); //glBindTexture( GL_TEXTURE_2D, GRPF.glTexture );
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glAlphaFunc(GL_GEQUAL, 0.5);
glEnable(GL_ALPHA_TEST);
aa = glGetError(); aa = glGetError();
renderConsoleGL();
/*
glBegin(GL_QUADS); glBegin(GL_QUADS);
glTexCoord2f(1-0 , 1-0); glVertex2f(512/200f - 1, 1 - 512/120f); glTexCoord2f(1-0 , 1-0); glVertex2f(512/200f - 1, 1 - 512/120f);
glTexCoord2f(1-0 , 1-1); glVertex2f(512/200f - 1, 1); glTexCoord2f(1-0 , 1-1); glVertex2f(512/200f - 1, 1);
glTexCoord2f(1-1 , 1-1); glVertex2f(-1, 1); glTexCoord2f(1-1 , 1-1); glVertex2f(-1, 1);
glTexCoord2f(1-1 , 1-0); glVertex2f(-1, 1 - 512/120f); glTexCoord2f(1-1 , 1-0); glVertex2f(-1, 1 - 512/120f);
glEnd(); glFlush(); glEnd(); glFlush();*/
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
auto a = (SDL_GetTicks() - profile);
if(!renderprofile) writeln(a);
while (SDL_PollEvent(&event)) while (SDL_PollEvent(&event))
{ {
switch (event.type) switch (event.type)
@@ -456,30 +462,33 @@ class PetitComputer
} }
} }
continue; continue;
auto profile = SDL_GetTicks(); version(none)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
renderConsole;
SDL_RenderPresent(renderer);
auto a = (SDL_GetTicks() - profile);
while (SDL_PollEvent(&event))
{ {
switch (event.type) auto profile = SDL_GetTicks();
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
renderConsole;
SDL_RenderPresent(renderer);
auto a = (SDL_GetTicks() - profile);
while (SDL_PollEvent(&event))
{ {
case SDL_QUIT: switch (event.type)
SDL_DestroyWindow(window); {
SDL_Quit(); case SDL_QUIT:
return; SDL_DestroyWindow(window);
case SDL_KEYDOWN: SDL_Quit();
auto key = event.key.keysym.sym; return;
keybuffer[keybufferpos] = cast(wchar)key; case SDL_KEYDOWN:
keybufferpos = (keybufferpos + 1) % keybuffer.length; auto key = event.key.keysym.sym;
break; keybuffer[keybufferpos] = cast(wchar)key;
default: keybufferpos = (keybufferpos + 1) % keybuffer.length;
break; break;
default:
break;
}
} }
if(!renderprofile) writeln(a);
} }
if(!renderprofile) writeln(a);
} }
} }
catch(Throwable t) catch(Throwable t)
@@ -521,7 +530,7 @@ WHILE 1
LOCATE 9,4+I,Z : COLOR (CL+I) MOD 16 LOCATE 9,4+I,Z : COLOR (CL+I) MOD 16
PRINT "★ 梅雨で雨が多い季節ですね ★" PRINT "★ 梅雨で雨が多い季節ですね ★"
NEXT NEXT
CL=(CL+1) MOD 16 : VSYNC 2 CL=(CL+1) MOD 16
WEND`*/ WEND`*/
/* /*
`CLS : CL=0 `CLS : CL=0
@@ -599,43 +608,50 @@ GOTO @LOOP`*/
int CSRZ; int CSRZ;
int consoleForeColor, consoleBackColor; int consoleForeColor, consoleBackColor;
Mutex consolem; Mutex consolem;
void renderConsole()
void renderConsoleGL()
{ {
consolem.lock(); consolem.lock();
scope(exit) consolem.unlock(); scope(exit) consolem.unlock();
glBindTexture(GL_TEXTURE_2D, GRPF.glTexture);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
for(int y = 0; y < consoleHeight; y++) for(int y = 0; y < consoleHeight; y++)
for(int x = 0; x < consoleWidth; x++) for(int x = 0; x < consoleWidth; x++)
{ {
SDL_Rect rect = SDL_Rect(x * 8, y * 8, 8, 8); auto back = consoleColorGL[console[y][x].backColor];
auto fore = consoleColor[console[y][x].foreColor]; glColor4ubv(cast(ubyte*)&back);
auto back = consoleColor[console[y][x].backColor]; glVertex3f((x * 8) / 200f - 1, 1 - (y * 8 + 8) / 120f, 0.9f);
SDL_SetTextureColorMod(t8x8, back >> 16 & 0xFF, back >> 8 & 0xFF, back & 0xFF); glVertex3f((x * 8) / 200f - 1, 1 - (y * 8) / 120f, 0.9f);
SDL_SetTextureAlphaMod(t8x8, back >> 24 & 0xFF); glVertex3f((x * 8 + 8) / 200f - 1, 1 - (y * 8) / 120f, 0.9f);
SDL_RenderCopy(renderer, t8x8, null, &rect); glVertex3f((x * 8 + 8) / 200f - 1, 1 - (y * 8 + 8) / 120f, 0.9f);
/*
auto texture = GRPFColor[back].texture;
auto back = console[y][x].backColor;
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);*/
} }
glEnd();
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//glAlphaFunc(GL_GEQUAL, 0.5);
//glEnable(GL_ALPHA_TEST);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
for(int y = 0; y < consoleHeight; y++)
for(int x = 0; x < consoleWidth; x++)
{
auto fore = consoleColorGL[console[y][x].foreColor];
auto rect = &fontTable[console[y][x].charater];
glColor4ubv(cast(ubyte*)&fore);
glTexCoord2f((rect.x) / 512f - 1 , (rect.y + 8) / 512f - 1);
glVertex3f((x * 8) / 200f - 1, 1 - (y * 8 + 8) / 120f, 0);
glTexCoord2f((rect.x) / 512f - 1, (rect.y) / 512f - 1);
glVertex3f((x * 8) / 200f - 1, 1 - (y * 8) / 120f, 0);
glTexCoord2f((rect.x + 8) / 512f - 1, (rect.y) / 512f - 1);
glVertex3f((x * 8 + 8) / 200f - 1, 1 - (y * 8) / 120f, 0);
glTexCoord2f((rect.x + 8) / 512f - 1, (rect.y +8) / 512f - 1);
glVertex3f((x * 8 + 8) / 200f - 1, 1 - (y * 8 + 8) / 120f, 0);
}
glEnd();
glFlush();
} }
void printConsole(T...)(T args) void printConsole(T...)(T args)
{ {