aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/render
diff options
context:
space:
mode:
Diffstat (limited to 'src/libjin/render')
-rw-r--r--src/libjin/render/drawable.cpp4
-rw-r--r--src/libjin/render/font.cpp2
-rw-r--r--src/libjin/render/font.h2
-rw-r--r--src/libjin/render/graphics.cpp3
-rw-r--r--src/libjin/render/jsl.cpp51
-rw-r--r--src/libjin/render/jsl.h9
-rw-r--r--src/libjin/render/quad.h17
-rw-r--r--src/libjin/render/rect.h12
-rw-r--r--src/libjin/render/render.h2
-rw-r--r--src/libjin/render/window.cpp23
-rw-r--r--src/libjin/render/window.h5
11 files changed, 60 insertions, 70 deletions
diff --git a/src/libjin/render/drawable.cpp b/src/libjin/render/drawable.cpp
index 4ac4f03..7880fd4 100644
--- a/src/libjin/render/drawable.cpp
+++ b/src/libjin/render/drawable.cpp
@@ -1,5 +1,5 @@
#include "drawable.h"
-#include "../utils/matrix.h"
+#include "../math/matrix.h"
#include <stdlib.h>
namespace jin
@@ -41,7 +41,7 @@ namespace render
// Must set textCoord and vertCoord before renderring
if (! textCoord||! vertCoord) return;
- static jin::util::Matrix t;
+ static jin::math::Matrix t;
t.setTransformation(x, y, r, sx, sy, ancx, ancy);
glEnable(GL_TEXTURE_2D);
diff --git a/src/libjin/render/font.cpp b/src/libjin/render/font.cpp
index 8a96f25..fa3e265 100644
--- a/src/libjin/render/font.cpp
+++ b/src/libjin/render/font.cpp
@@ -11,6 +11,8 @@ namespace jin
namespace render
{
+ using namespace jin::math;
+
#define BITMAP_WIDTH 512
#define BITMAP_HEIGHT 512
#define PIXEL_HEIGHT 32
diff --git a/src/libjin/render/font.h b/src/libjin/render/font.h
index 98a41b4..55f0a44 100644
--- a/src/libjin/render/font.h
+++ b/src/libjin/render/font.h
@@ -3,7 +3,7 @@
#include "drawable.h"
#include "3rdparty/stb/stb_truetype.h"
-#include "quad.h"
+#include "../math/quad.h"
namespace jin
{
diff --git a/src/libjin/render/graphics.cpp b/src/libjin/render/graphics.cpp
index 254b6a4..a4ccd66 100644
--- a/src/libjin/render/graphics.cpp
+++ b/src/libjin/render/graphics.cpp
@@ -1,5 +1,6 @@
#include "graphics.h"
-#include "../utils/math.h"
+#include "../math/matrix.h"
+#include "../math/constant.h"
#include <string>
namespace jin
{
diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp
index 3702e14..e883104 100644
--- a/src/libjin/render/jsl.cpp
+++ b/src/libjin/render/jsl.cpp
@@ -6,25 +6,23 @@ namespace render
{
//vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords)
static const char* base_f = " "
- "#version 120 \n"
- "#define number float \n"
- "#define Image sampler2D \n"
- "#define Canvas sampler2D \n"
- "#define Color vec4 \n"
- "#define Texel texture2D \n"
- "#define extern uniform \n"
- "uniform Image _tex0_; \n"
- "%s \n"
- "void main(){ \n"
+ "#version 120 \n"
+ "#define number float \n"
+ "#define Image sampler2D \n"
+ "#define Canvas sampler2D \n"
+ "#define Color vec4 \n"
+ "#define Texel texture2D \n"
+ "#define extern uniform \n"
+ "uniform Image _tex0_; \n"
+ "%s \n"
+ "void main(){ \n"
"gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n"
"}\0";
- shared GLint JSLProgram::currentTextureUnit = 0;
- shared GLint JSLProgram::maxTextureUnits = -1;
-
shared JSLProgram* JSLProgram::currentJSLProgram = nullptr;
JSLProgram::JSLProgram(const char* program)
+ : currentTextureUnit(0)
{
initialize(program);
}
@@ -42,9 +40,6 @@ namespace render
inline void JSLProgram::initialize(const char* program)
{
- if (maxTextureUnits == -1)
- glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
-
char* fs = (char*)alloca(strlen(program) + strlen(base_f));
sprintf(fs, base_f, program);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
@@ -55,12 +50,20 @@ namespace render
glAttachShader(pid, fragmentShader);
glLinkProgram(pid);
}
+
+ static inline GLint getMaxTextureUnits()
+ {
+ GLint maxTextureUnits = 0;
+ glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
+ return maxTextureUnits;
+ }
GLint JSLProgram::getTextureUnit(const std::string& name)
{
std::map<std::string, GLint>::iterator texture_unit = texturePool.find(name);
if (texture_unit != texturePool.end())
return texture_unit->second;
+ static GLint maxTextureUnits = getMaxTextureUnits();
if (++currentTextureUnit >= maxTextureUnits)
return 0;
texturePool[name] = currentTextureUnit;
@@ -81,14 +84,13 @@ namespace render
{
checkJSL();
- GLint texture_unit = JSLProgram::getTextureUnit(variable);
-
GLint location = glGetUniformLocation(pid, variable);
+ if (location == -1)
+ return;
+ GLint texture_unit = getTextureUnit(variable);
glUniform1i(location, texture_unit);
-
glActiveTexture(GL_TEXTURE0 + texture_unit);
- glBindTexture(GL_TEXTURE_2D, image->getTexture());
-
+ glBindTexture(GL_TEXTURE_2D, image->getTexture());
glActiveTexture(GL_TEXTURE0);
}
@@ -96,14 +98,13 @@ namespace render
{
checkJSL();
- GLint texture_unit = getTextureUnit(variable);
-
GLint location = glGetUniformLocation(pid, variable);
+ if (location == -1)
+ return;
+ GLint texture_unit = getTextureUnit(variable);
glUniform1i(location, texture_unit);
-
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, canvas->getTexture());
-
glActiveTexture(GL_TEXTURE0);
}
diff --git a/src/libjin/render/jsl.h b/src/libjin/render/jsl.h
index 35479d3..fc1aa48 100644
--- a/src/libjin/render/jsl.h
+++ b/src/libjin/render/jsl.h
@@ -24,14 +24,12 @@ namespace render
{
glUseProgram(pid);
currentJSLProgram = this;
- currentTextureUnit = 0;
}
static inline void JSLProgram::unuse()
{
glUseProgram(0);
currentJSLProgram = nullptr;
- currentTextureUnit = 0;
}
void sendFloat(const char* name, float number);
@@ -49,14 +47,13 @@ namespace render
private:
+ static JSLProgram* currentJSLProgram;
+
GLuint pid;
std::map<std::string, GLint> texturePool;
- static JSLProgram* currentJSLProgram;
- static GLint currentTextureUnit;
- static GLint maxTextureUnits;
-
+ GLint currentTextureUnit;
GLint getTextureUnit(const std::string& name);
inline void initialize(const char* program);
diff --git a/src/libjin/render/quad.h b/src/libjin/render/quad.h
deleted file mode 100644
index 3ae4cc4..0000000
--- a/src/libjin/render/quad.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef __JIN_QUAD_H
-#define __JIN_QUAD_H
-
-namespace jin
-{
-namespace render
-{
-
- struct Quad
- {
- float x, y, w, h;
- };
-
-}
-}
-
-#endif // !__JIN_RENDER_QUAD_H
diff --git a/src/libjin/render/rect.h b/src/libjin/render/rect.h
deleted file mode 100644
index 56b5bd1..0000000
--- a/src/libjin/render/rect.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef __JIN_RECT_H
-#define __JIN_RECT_H
-
-namespace jin
-{
-class Rect
-{
-public:
- int x, y, w, h;
-};
-}// jin
-#endif \ No newline at end of file
diff --git a/src/libjin/render/render.h b/src/libjin/render/render.h
index 8939480..6c0d6bf 100644
--- a/src/libjin/render/render.h
+++ b/src/libjin/render/render.h
@@ -7,8 +7,6 @@
#include "graphics.h"
#include "image.h"
#include "jsl.h"
-#include "quad.h"
-#include "rect.h"
#include "window.h"
#endif \ No newline at end of file
diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp
index 1f55be8..1e58dd8 100644
--- a/src/libjin/render/window.cpp
+++ b/src/libjin/render/window.cpp
@@ -8,8 +8,8 @@ namespace jin
namespace render
{
- shared Window* Window::g_wnd = 0;
-
+ shared Window* Window::g_wnd = NULL;
+
Window::Window(): wnd(0), ctx(0)
{
}
@@ -18,12 +18,23 @@ namespace render
{
}
+ bool Window::init(const SettingBase* setting)
+ {
+ static bool result = _init(setting);
+ return result;
+ }
+
+ void Window::quit()
+ {
+ CallOnce(_quit());
+ }
+
onlyonce bool Window::_init(const SettingBase* s)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return false;
- WindowSetting* setting = (WindowSetting*)s;
+ const WindowSetting* setting = (WindowSetting*)s;
width = setting->width;
height = setting->height;
@@ -73,6 +84,12 @@ namespace render
return true;
}
+ onlyonce void Window::_quit()
+ {
+ SDL_DestroyWindow(wnd);
+ delete g_wnd;
+ }
+
SDL_Window* Window::getWnd()
{
return wnd;
diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h
index 1064c36..1aabfa0 100644
--- a/src/libjin/render/window.h
+++ b/src/libjin/render/window.h
@@ -42,6 +42,9 @@ namespace render
inline void swapBuffers();
+ bool init(const SettingBase* setting) override;
+ void quit() override;
+
private:
Window();
@@ -56,7 +59,7 @@ namespace render
int width, height;
onlyonce bool _init(const SettingBase* setting) override;
-
+ onlyonce void _quit() override;
};
typedef Window::Setting WindowSetting;