aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-09-20 12:01:54 +0800
committerchai <chaifix@163.com>2018-09-20 12:01:54 +0800
commit54f1e742ce9de35013929de6b02948d59db89ccf (patch)
tree59d901cc74e5859a739711c87203e5c95c19b527
parent6e81b7ddf8c0b378e983c1d2a1fb6f4b43d70d9a (diff)
*update
-rw-r--r--build/05Font/05Font.vcxproj2
-rw-r--r--build/libjin/libjin.vcxproj2
-rw-r--r--libjin/3rdparty/ogl/OpenGL.h70
-rw-r--r--libjin/Graphics/Canvas.cpp4
-rw-r--r--libjin/Graphics/Drawable.cpp8
-rw-r--r--libjin/Graphics/Font.cpp36
-rw-r--r--libjin/Graphics/Font.h2
-rw-r--r--libjin/Graphics/FontData.cpp22
-rw-r--r--libjin/Graphics/Shader.cpp2
-rw-r--r--libjin/Graphics/Texture.cpp5
-rw-r--r--libjin/Graphics/Window.cpp14
-rw-r--r--test/05Font/main.cpp8
12 files changed, 100 insertions, 75 deletions
diff --git a/build/05Font/05Font.vcxproj b/build/05Font/05Font.vcxproj
index 2522097..3bdac9b 100644
--- a/build/05Font/05Font.vcxproj
+++ b/build/05Font/05Font.vcxproj
@@ -22,7 +22,7 @@
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{D1953718-E728-4A86-9CCF-8BEC1F5C5F97}</ProjectGuid>
<RootNamespace>My05Font</RootNamespace>
- <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
diff --git a/build/libjin/libjin.vcxproj b/build/libjin/libjin.vcxproj
index cba0565..0cdd2ad 100644
--- a/build/libjin/libjin.vcxproj
+++ b/build/libjin/libjin.vcxproj
@@ -128,7 +128,7 @@
<ProjectGuid>{407E9199-D39C-4460-B218-0C29AB42483B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>libjin</RootNamespace>
- <WindowsTargetPlatformVersion>10.0.14393.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
diff --git a/libjin/3rdparty/ogl/OpenGL.h b/libjin/3rdparty/ogl/OpenGL.h
index dcd2168..4789662 100644
--- a/libjin/3rdparty/ogl/OpenGL.h
+++ b/libjin/3rdparty/ogl/OpenGL.h
@@ -1,7 +1,8 @@
#ifndef __OGL2D_H
#define __OGL2D_H
#include <vector>
-//#include "../GLee/GLee.h"
+
+/* include gl.h before this file */
/* 2d wrap of opengl 3.0 */
class OpenGL
@@ -10,9 +11,28 @@ public:
OpenGL();
~OpenGL();
- void pushColor(unsigned int r, unsigned int g, unsigned int b, unsigned int a);
+ inline void enable(GLenum cap)
+ {
+ glEnable(cap);
+ }
+
+ inline void disable(GLenum cap)
+ {
+ glDisable(cap);
+ }
+
+ inline void setBlendFunc(GLenum sfactor, GLenum dfactor)
+ {
+ glBlendFunc(sfactor, dfactor);
+ }
+
+ inline void setClearColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a)
+ {
+ glClearColor(r/255.f, g/255.f, b/255.f, a/255.f);
+ }
+
+ void pushColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a = 255);
void popColor();
- void flushColor();
void flushError();
GLuint genTexture();
void bindTexture(GLuint texture = 0);
@@ -21,8 +41,6 @@ public:
return _texture;
}
void setTexParameter(GLenum pname, GLint param);
- void drawColor8i(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
- void drawColor32f(float r, float g, float b, float a);
void texImage(GLint internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels = NULL);
void texSubImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
void activeTexUnit(unsigned int unit = 0);
@@ -51,9 +69,20 @@ public:
}
+ inline void enableClientState(GLenum arr)
+ {
+ glEnableClientState(arr);
+ }
+
+ inline void disableClientState(GLenum arr)
+ {
+ glDisableClientState(arr);
+ }
+
private:
- std::vector<unsigned int> _color;
- GLuint _texture;
+ struct { GLubyte r, g, b, a; } _color; // current draw color
+ struct { GLubyte r, g, b, a; } _precolor; // previous draw color
+ GLuint _texture; // current binded texture
};
@@ -66,7 +95,8 @@ OpenGL gl;
OpenGL::OpenGL()
{
- _color.push_back((0xff << 24) | (0xff << 16) | (0xff << 8) | 0xff);
+ memset(&_color, 0xff, sizeof(_color));
+ memset(&_precolor, 0xff, sizeof(_precolor));
}
OpenGL::~OpenGL()
@@ -74,26 +104,20 @@ OpenGL::~OpenGL()
}
-void OpenGL::pushColor(unsigned int r, unsigned int g, unsigned int b, unsigned int a)
+void OpenGL::pushColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a)
{
- unsigned int c = (r << 24) | (g << 16) | (b << 8) | a;
- _color.push_back(c);
- glColor4i(r, g, b, a);
+ memcpy(&_precolor, &_color, sizeof(_precolor));
+ _color.r = r;
+ _color.g = g;
+ _color.b = b;
+ _color.a = a;
+ glColor4ub(r, g, b, a);
}
void OpenGL::popColor()
{
- if (_color.size() == 1)
- return;
- _color.pop_back();
- unsigned int c = _color.back();
- glColor4i((c >> 24) & 0xff, (c >> 16) & 0xff, (c >> 8) & 0xff, c & 0xff);
-}
-
-void OpenGL::flushColor()
-{
- while (_color.size() != 1)
- _color.pop_back();
+ memcpy(&_color, &_precolor, sizeof(_precolor));
+ glColor4ub(_color.r, _color.g, _color.b, _color.a);
}
void OpenGL::flushError()
diff --git a/libjin/Graphics/Canvas.cpp b/libjin/Graphics/Canvas.cpp
index 6164ffe..511aa12 100644
--- a/libjin/Graphics/Canvas.cpp
+++ b/libjin/Graphics/Canvas.cpp
@@ -35,11 +35,11 @@ namespace graphics
/* generate texture save target */
glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
+ gl.bindTexture(texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
- glBindTexture(GL_TEXTURE_2D, 0);
+ gl.bindTexture(0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
diff --git a/libjin/Graphics/Drawable.cpp b/libjin/Graphics/Drawable.cpp
index 3ba4717..412b900 100644
--- a/libjin/Graphics/Drawable.cpp
+++ b/libjin/Graphics/Drawable.cpp
@@ -50,9 +50,9 @@ namespace graphics
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glVertexPointer(2, GL_FLOAT, 0, vertex_coords);
- glTexCoordPointer(2, GL_FLOAT, 0, texture_coords);
- glDrawArrays(GL_QUADS, 0, 4);
+ gl.vertexPointer(2, GL_FLOAT, 0, vertex_coords);
+ gl.texCoordPointer(2, GL_FLOAT, 0, texture_coords);
+ gl.drawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
@@ -60,7 +60,7 @@ namespace graphics
glPopMatrix();
/* bind texture to default screen */
- glBindTexture(GL_TEXTURE_2D, 0);
+ gl.bindTexture(0);
}
} // render
diff --git a/libjin/Graphics/Font.cpp b/libjin/Graphics/Font.cpp
index b4fb76c..305c735 100644
--- a/libjin/Graphics/Font.cpp
+++ b/libjin/Graphics/Font.cpp
@@ -127,7 +127,7 @@ namespace graphics
Page* Font::typeset(const char* t, int x, int y, int lineheight, int spacing)
{
- // typesetting, for reduce draw call
+ // typesetting, for reducing draw call
Page* page = new Page();
vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
vector<GlyphVertex>& glyphvertices = page->glyphvertices;
@@ -165,20 +165,20 @@ namespace graphics
glyphinfolist[glyphinfolist.size() - 1].count += 4;
Glyph::Bbox& bbox = glyph->bbox;
// 1
- vertex.x = p.x; vertex.u = bbox.x;
- vertex.y = p.y; vertex.v = bbox.y;
+ vertex.x = p.x; vertex.y = p.y;
+ vertex.u = bbox.x; vertex.v = bbox.y;
glyphvertices.push_back(vertex);
// 2
- vertex.x = p.x; vertex.u = bbox.x;
- vertex.y = p.y + glyph->height; vertex.v = bbox.y + bbox.height;
+ vertex.x = p.x; vertex.y = p.y + glyph->height;
+ vertex.u = bbox.x; vertex.v = bbox.y + bbox.height;
glyphvertices.push_back(vertex);
// 3
- vertex.x = p.x + glyph->width; vertex.u = bbox.x + bbox.width;
- vertex.y = p.y + glyph->height; vertex.v = bbox.y + bbox.height;
+ vertex.x = p.x + glyph->width; vertex.y = p.y + glyph->height;
+ vertex.u = bbox.x + bbox.width; vertex.v = bbox.y + bbox.height;
glyphvertices.push_back(vertex);
// 4
- vertex.x = p.x + glyph->width; vertex.u = bbox.x + bbox.width;
- vertex.y = p.y; vertex.v = bbox.y;
+ vertex.x = p.x + glyph->width; vertex.y = p.y;
+ vertex.u = bbox.x + bbox.width; vertex.v = bbox.y;
glyphvertices.push_back(vertex);
p.x += glyph->width + spacing;
@@ -190,8 +190,8 @@ namespace graphics
{
const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ gl.enableClientState(GL_VERTEX_ARRAY);
+ gl.enableClientState(GL_TEXTURE_COORD_ARRAY);
for (int i = 0; i < glyphinfolist.size(); ++i)
{
const GlyphArrayDrawInfo& info = glyphinfolist[i];
@@ -205,7 +205,7 @@ namespace graphics
gl.bindTexture(0);
}
#if defined(font_debug)
- glBindTexture(GL_TEXTURE_2D, 1);
+ gl.bindTexture(1);
float xy[] = {
0,0,
0,textureHeight,
@@ -218,13 +218,13 @@ namespace graphics
1, 1,
1, 0
};
- glVertexPointer(2, GL_FLOAT, 0, xy);
- glTexCoordPointer(2, GL_FLOAT, 0, uv);
- glDrawArrays(GL_QUADS, 0, 4);
- glBindTexture(GL_TEXTURE_2D, 0);
+ gl.vertexPointer(2, GL_FLOAT, 0, xy);
+ gl.texCoordPointer(2, GL_FLOAT, 0, uv);
+ gl.drawArrays(GL_QUADS, 0, 4);
+ gl.bindTexture(0);
#endif
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+ gl.disableClientState(GL_VERTEX_ARRAY);
+ gl.disableClientState(GL_TEXTURE_COORD_ARRAY);
}
//float Font::getCharWidth(int c, int last) {
diff --git a/libjin/Graphics/Font.h b/libjin/Graphics/Font.h
index b09d393..34e53c8 100644
--- a/libjin/Graphics/Font.h
+++ b/libjin/Graphics/Font.h
@@ -55,7 +55,7 @@ namespace graphics
Page* typeset(const char* text, int x, int y, int lineheight, int spacing);
void render(const Page* page);
- void print(const char* text, int x, int y, int lineheight, int spacing);
+ void print(const char* text, int x, int y, int lineheight, int spacing = 0);
//Bitmap* bake(const char* text);
#if defined(font_debug)
void drawAtlas();
diff --git a/libjin/Graphics/FontData.cpp b/libjin/Graphics/FontData.cpp
index 168ac79..1b66b12 100644
--- a/libjin/Graphics/FontData.cpp
+++ b/libjin/Graphics/FontData.cpp
@@ -43,17 +43,17 @@ namespace graphics
/*
* (0, 0)
- * +--------------+ ascent
- * | +--------+ |
- * | | | |
- * | | bitmap | |
- * +--|--------|--+ baseline
- * | +--------+ |
- * +--|-----------+ decent
- * | |
- * leftSideBearing|
- * |
- * advanceWidth
+ * +--------------+ ascent
+ * | +--------+ |
+ * | | | |
+ * | | bitmap | |
+ * +--|--------|--+ baseline
+ * | +--------+ |
+ * +--|-----------+ decent
+ * | |
+ * leftSideBearing |
+ * |
+ * advanceWidth
*/
void FontData::getVMetrics(int* baseline, int* descent)
{
diff --git a/libjin/Graphics/Shader.cpp b/libjin/Graphics/Shader.cpp
index 252401f..f514169 100644
--- a/libjin/Graphics/Shader.cpp
+++ b/libjin/Graphics/Shader.cpp
@@ -177,7 +177,7 @@ namespace graphics
}
glUniform1i(location, unit);
glActiveTexture(GL_TEXTURE0 + unit);
- glBindTexture(GL_TEXTURE_2D, canvas->getTexture());
+ gl.bindTexture(canvas->getTexture());
glActiveTexture(GL_TEXTURE0);
}
diff --git a/libjin/Graphics/Texture.cpp b/libjin/Graphics/Texture.cpp
index 4d73f76..5a39f77 100644
--- a/libjin/Graphics/Texture.cpp
+++ b/libjin/Graphics/Texture.cpp
@@ -30,9 +30,8 @@ namespace graphics
gl.bindTexture(texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
-
- glBindTexture(GL_TEXTURE_2D, 0);
+ gl.texImage(GL_RGBA8, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+ gl.bindTexture(0);
}
Texture::~Texture()
diff --git a/libjin/Graphics/Window.cpp b/libjin/Graphics/Window.cpp
index c3743d3..d04a1c7 100644
--- a/libjin/Graphics/Window.cpp
+++ b/libjin/Graphics/Window.cpp
@@ -68,11 +68,11 @@ namespace graphics
SDL_GL_SetSwapInterval(vsync ? 1 : 0);
SDL_GL_MakeCurrent(wnd, ctx);
/* default configuration */
- glClearColor(0.f, 0.f, 0.f, 1.f);
- glColor4f(1, 1, 1, 1);
- glEnable(GL_BLEND);
- glEnable(GL_TEXTURE_2D);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ gl.setClearColor(0, 0, 0, 0xff);
+ gl.pushColor(0xff, 0xff, 0xff, 0xff);
+ gl.enable(GL_BLEND);
+ gl.enable(GL_TEXTURE_2D);
+ gl.setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* avoid white screen blink on windows */
swapBuffers();
/* bind to default canvas */
@@ -83,8 +83,8 @@ namespace graphics
void Window::quitSystem()
{
/* disable opengl */
- glDisable(GL_BLEND);
- glDisable(GL_TEXTURE_2D);
+ gl.disable(GL_BLEND);
+ gl.disable(GL_TEXTURE_2D);
/* close window */
SDL_DestroyWindow(wnd);
SDL_Quit();
diff --git a/test/05Font/main.cpp b/test/05Font/main.cpp
index 51dc628..cc6b0e6 100644
--- a/test/05Font/main.cpp
+++ b/test/05Font/main.cpp
@@ -25,9 +25,9 @@ Color effect(Color col, Texture tex, vec2 uv, vec2 screen)
Filesystem* fs = Filesystem::get();
fs->mount("../Debug");
Buffer buffer;
- fs->read("font2.ttf", &buffer);
+ fs->read("font.ttf", &buffer);
data = FontData::createFontData((const unsigned char*)buffer.data, buffer.size);
- font = Font::createFont(data, 14);
+ font = Font::createFont(data, 16);
//canvas = Canvas::createCanvas(100, 100);
//page = font->typeset("こんにちは世界!", 120, 20);
}
@@ -46,6 +46,7 @@ void onUpdate(int ms)
void onDraw()
{
+ gl.pushColor(32, 32, 32, 255);
glColor4f(32 / 255.f, 32 / 255.f, 32 / 255.f, 1);
rect(FILL, 0, 0, 500, 500);
glColor4f(1, 1, 1, 1);
@@ -58,6 +59,7 @@ void onDraw()
//font->print(u8"Hello,你好\n啊 world!", 10, 10);
//font->print(u8"Привет мир!", 10, 10 + 15 * 1);
font->print(u8R"(
+One of the most enjoyable arcade game.
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
平安時代中期の物語。紫式部著。ただし,そのすべてが紫式部の筆に成るのでは
ないとする説もある。 54帖。寛弘 (1004~12) 頃成立か。物語は3部に分けてみ
@@ -75,7 +77,7 @@ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
》开始在日本东京电视台播出。2004年,漫画进而改编成电影。2006年,漩涡鸣人入选
美国《新闻周刊》日文版于10月18日发行的特集中选出的“全世界最受尊敬的100位日本
人”。[2]
-)", 10, 10 + 15 * 2, 17, 0);
+)", 10, 10 + 15 * 2, 17,1);
//font->print(u8"你好世界!", 10, 10 + 15*3);
//font->render(page);
glColor4f(1, 1, 1, 1);