Skip to content

Commit

Permalink
Handling window resizing events
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroBone committed Feb 24, 2019
1 parent a3c128b commit 9b63087
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 11 deletions.
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -7,4 +7,10 @@ It's goal is also to demonstrate **floating-point precision errors**, that's why
* Scaling with mouse wheel.
* No rendering if the math function returns `NaN` or `Infinity`.
* Movable axises. Screen => Math, Math => Screen unit converters.
* Automatically calculate scale and axis position based on interval of the math function.
* Automatically calculate scale and axis position based on interval of the math function.
* Pixel perfect rendering.

## Demo
![Sin demo](/screenshots/sin.png)
![Log demo](/screenshots/log.png)
![Sin2 demo](/screenshots/sin2.png)
2 changes: 1 addition & 1 deletion main.cpp
Expand Up @@ -24,7 +24,7 @@ double mathF(double x) {

int main(int argc, char* argv[]) {

Grapher* grapher = new Grapher(640, 480, mathF);
Grapher* grapher = new Grapher(mathF);

grapher->init("Grapher", "arial.ttf");

Expand Down
Binary file added screenshots/log.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/sin.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/sin2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions src/App.c++
@@ -1,8 +1,8 @@
#include "App.h"
#include <SDL_ttf.h>

App::App(int vWidth, int vHeight): vWidth(vWidth), vHeight(vHeight) {}
App::~App() {}
App::App() = default;
App::~App() = default;

void App::init(const char* title) {

Expand All @@ -12,13 +12,21 @@ void App::init(const char* title) {
}

// window
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, vWidth, vHeight, SDL_WINDOW_OPENGL);
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640, 480,
SDL_WINDOW_OPENGL | SDL_WINDOW_MAXIMIZED | SDL_WINDOW_RESIZABLE
);

if (!window) {
running = false;
return;
}

// SDL_GetWindowSize(window, &vWidth, &vHeight);

// renderer
renderer = SDL_CreateRenderer(window, -1, 0);

Expand All @@ -27,6 +35,8 @@ void App::init(const char* title) {
return;
}

SDL_GetRendererOutputSize(renderer, &vWidth, &vHeight);

if (TTF_Init() < 0) {
running = false;
return;
Expand Down
11 changes: 10 additions & 1 deletion src/App.h
Expand Up @@ -2,6 +2,7 @@
#define ZAVU_APP_H

#include <SDL.h>
#include <iostream>

class App {

Expand All @@ -16,7 +17,7 @@ class App {

public:

App(int vWidth, int vHeight);
App();
~App();

virtual void init(const char* title);
Expand All @@ -27,6 +28,14 @@ class App {

}

virtual void onResized() {

// std::cout << "resized" << std::endl;

SDL_GetRendererOutputSize(renderer, &vWidth, &vHeight);

}

void destroy() {

SDL_DestroyRenderer(renderer);
Expand Down
27 changes: 27 additions & 0 deletions src/Grapher.c++
Expand Up @@ -127,10 +127,22 @@ void Grapher::init(const char* title, const char* fontFile) {

App::init(title);

cx = vWidth / 2;
cy = vHeight / 2;

labelFont = TTF_OpenFont(fontFile, 10);

}

void Grapher::onResized() {

App::onResized();

cx = vWidth / 2;
cy = vHeight / 2;

}

void Grapher::renderGraph() {

SDL_SetRenderDrawColor(renderer, 0xff, 0xa0, 0xa0, 0xff);
Expand Down Expand Up @@ -178,6 +190,8 @@ void Grapher::renderGraph() {

void Grapher::render() {

// std::cout << "W: " << vWidth << " H: " << vHeight << std::endl;

renderAxes();

renderGraph();
Expand All @@ -192,6 +206,19 @@ void Grapher::handleEvents() {

switch (currentEvent.type) {

case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
onResized();
break;

case SDL_WINDOWEVENT:

if (currentEvent.window.event == SDL_WINDOWEVENT_RESIZED) {
onResized();
}

break;

case SDL_QUIT:
running = false;
break;
Expand Down
7 changes: 2 additions & 5 deletions src/Grapher.h
Expand Up @@ -19,16 +19,13 @@ class Grapher : public App {

public:

Grapher(int vWidth, int vHeight, mathFunction_t mathFunction) :
mathFunction(mathFunction),
cx(vWidth / 2),
cy(vHeight / 2),
App(vWidth, vHeight) {}
Grapher(mathFunction_t mathFunction) : mathFunction(mathFunction) {}

~Grapher();

void run();
void init(const char* title, const char* fontFile);
void onResized();

private:

Expand Down

0 comments on commit 9b63087

Please sign in to comment.