Browse Source

resizing

opengl-renderer-broken
Martin Dørum 3 years ago
parent
commit
72399ea76c

+ 6
- 11
libcygnet/include/cygnet/RenderQueue.h View File

@@ -30,17 +30,12 @@ public:
queue_.push_back({ x, y, w * pixScale_, h * pixScale_, tex });
}

float pixScale() { return pixScale_; }
void pixScale(float scale) { pixScale_ = scale; }

float scaleX() { return mat_[0]; }
void scaleX(float sx) { mat_[0] = sx; }
float scaleY() { return -mat_[4]; }
void scaleY(float sy) { mat_[4] = -sy; }
float translateX() { return mat_[2]; }
void translateX(float tx) { mat_[2] = tx; }
float translateY() { return mat_[5]; }
void translateY(float ty) { mat_[5] = ty; }
float getScaleX() { return mat_[0]; }
float getScaleY() { return -mat_[4]; }
float getTranslateX() { return mat_[3]; }
float getTranslateY() { return mat_[5]; }
void setScale(float sx, float sy) { mat_[0] = sx; mat_[4] = -sy; }
void setTranslate(float tx, float ty) { mat_[2] = tx; mat_[5] = ty; }

void draw();


+ 13
- 0
libcygnet/include/cygnet/Window.h View File

@@ -17,10 +17,23 @@ public:
void makeCurrent();
void clear();
void flip();
int width() { return w_; }
int height() { return h_; }

// xScale and yScale are what drawn objects need to be scaled with
// in order to have square pixels.
float xScale() { return xScale_; }
float yScale() { return yScale_; }

void onResize(int w, int h);

private:
CPtr<SDL_Window, SDL_DestroyWindow> win_;
SDL_GLContext glctx_;
int w_;
int h_;
float yScale_;
float xScale_;
};

}

+ 4
- 5
libcygnet/samples/game/game.cc View File

@@ -7,6 +7,7 @@
#include <stdio.h>
#include <memory>
#include <vector>
#include <iostream>

const char *vertexShader = R"(
uniform mat3 transform;
@@ -108,8 +109,7 @@ int main() {
Cygnet::GlShader(Cygnet::GlShader::Type::FRAGMENT, fragmentShader));
program.use();

Cygnet::RenderQueue q(program, 1/32.0);
q.scaleX(1 / (640.0 / 480.0));
Cygnet::RenderQueue q(program, 1/100.0);

State state{
.keys{},
@@ -131,9 +131,7 @@ int main() {

case SDL_WINDOWEVENT:
if (evt.window.event == SDL_WINDOWEVENT_RESIZED) {
glViewport(0, 0, evt.window.data1, evt.window.data2);
float ratio = (float)evt.window.data1 / (float)evt.window.data2;
q.scaleX(1 / ratio);
win.onResize(evt.window.data1, evt.window.data2);
}
break;

@@ -167,6 +165,7 @@ int main() {
ent->draw(state);
}

q.setScale(win.xScale(), win.yScale());
q.draw();
win.flip();
}

+ 11
- 0
libcygnet/src/Window.cc View File

@@ -27,6 +27,8 @@ Window::Window(const char *name, int width, int height) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glCheck();

onResize(width, height);
}

Window::~Window() {
@@ -46,4 +48,13 @@ void Window::flip() {
SDL_GL_SwapWindow(win_.get());
}

void Window::onResize(int w, int h) {
w_ = w;
h_ = h;
float ratio = (float)h / (float)w;
yScale_ = 1 / ratio;
xScale_ = 1;
glViewport(0, 0, w, h);
}

}

Loading…
Cancel
Save