summaryrefslogtreecommitdiffhomepage
path: root/src/main.cpp
blob: 5cca382ae09b052c559bfcb21b2230123eb76ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "glad/glad.h"
#include "GLFW/glfw3.h"

#include "input.hpp"
#include "shader.hpp"
#include "batch.hpp"
#include "window.hpp"
#include "random.hpp"

#include <iostream>
#include <cmath>
#include <chrono>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

typedef struct Bunny {
	glm::vec2 position;
	glm::vec2 speed;
	glm::vec4 color;
} Bunny;

const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 450;


int main() {
	Window::init(SCREEN_WIDTH, SCREEN_HEIGHT, "Ogle");

	Shader shader = Shader("src/shaders/default.vert", "src/shaders/default.frag");
	Batch batch = Batch(1024 * 8);
	Texture texture = Texture("assets/wabbit_alpha.png");

	glm::mat4 transform = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f, 0.1f, 100.0f);
	shader.use();
	shader.setMatrix4fv("transform", transform);

	auto start = std::chrono::high_resolution_clock::now();
	auto stop = std::chrono::high_resolution_clock::now();

	std::vector<Bunny> bunnies = std::vector<Bunny>{};
	const int MAX_BUNNIES = 50000;

	// game loop
	while(!glfwWindowShouldClose(Window::get()))
	{
		start = std::chrono::high_resolution_clock::now();
		Input::process(Window::get());

		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		shader.use();

		if(Input::get_mouse_click())
		{
			Bunny new_bunny;
			for (int i =0; i < 100; i++)
			{
				if (bunnies.size() < MAX_BUNNIES)
				{
					new_bunny = (Bunny){
						glm::vec2(Input::get_mouse_x(), Input::get_mouse_y()),
							glm::vec2((float)Random::get_value(-250, 250)/60.0f, (float)Random::get_value(-250, 250)/60.0f),
							glm::vec4(
									Random::get_value(50, 240) / 255.0,
									Random::get_value(50, 240) / 255.0,
									Random::get_value(100, 240) / 255.0,
									1.0f
									)
					};
					bunnies.push_back(new_bunny);
				}
			}
			//batch.drawRectangle(100,100,50.0,50.0,glm::vec4(0.3,0.3,1.0,1.0));
		}

		// move bunnies
		for (int i = 0; i < bunnies.size(); i++)
		{
			bunnies.at(i).position.x += bunnies.at(i).speed.x;
			bunnies.at(i).position.y += bunnies.at(i).speed.y;

			if (((bunnies.at(i).position.x) > SCREEN_WIDTH) || ((bunnies.at(i).position.x) < 0))
				bunnies.at(i).speed.x *= -1;
			if (((bunnies.at(i).position.y) > SCREEN_HEIGHT) || ((bunnies.at(i).position.y) < 0))
				bunnies.at(i).speed.y *= -1;
		}

		for (int i = 0; i < bunnies.size(); i++)
		{
			batch.drawTexture(
					texture,
					bunnies.at(i).position.x,
					bunnies.at(i).position.y,
					32,
					32,
					bunnies.at(i).color
					);
		}
		//batch.drawTexture(texture,0.0,0.0,1.0,1.0,glm::vec4(1.0,1.0,1.0,0.5));

		batch.flush_batch(); // does the drawing


		glfwSwapBuffers(Window::get());
		glfwPollEvents();
		stop = std::chrono::high_resolution_clock::now();

		auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);

		std::cout << bunnies.size();
		std::cout << " Bunnies at ";
		std::cout << round(1.0f/((float)duration.count() / 10000000.0f))/10.0;
		std::cout << " FPS";
		std::cout << std::endl;
	}

	glfwTerminate();
	return 0;
}