327 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			327 lines
		
	
	
		
			9.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "main_code.h"
 | |
| 
 | |
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <math.h>
 | |
| #include <time.h>
 | |
| 
 | |
| #include "include/Engine.h"
 | |
| 
 | |
| #include "main_code.h"
 | |
| 
 | |
| TMyApplication* Application;
 | |
| 
 | |
| //static const float BUFFER_DISTANCE = 30.f;
 | |
| static const float BUFFER_DISTANCE = 50.f;
 | |
| 
 | |
| 
 | |
| //#define USE_SHADER_X16
 | |
| 
 | |
| 
 | |
| #ifdef USE_SHADER_X16
 | |
| 
 | |
| 
 | |
| std::vector<TRenderPair> linesToRenderBoxesX16(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
 | |
| {
 | |
| 
 | |
| 	
 | |
| 	std::vector<TRenderPair> lineRenderPairArr;
 | |
| 
 | |
| 	size_t boxCount = lineArr.size() / 16;
 | |
| 
 | |
| 	if (lineArr.size() % 16 > 0)
 | |
| 	{
 | |
| 		boxCount++;
 | |
| 	}
 | |
| 
 | |
| 	lineRenderPairArr.resize(boxCount);
 | |
| 
 | |
| 	//Set some initial values:
 | |
| 	for (size_t i = 0; i < lineRenderPairArr.size(); i++)
 | |
| 	{
 | |
| 
 | |
| 		for (size_t j = 0; j < 16; j++)
 | |
| 		{
 | |
| 			lineRenderPairArr[i].first.Vec4Map["lineParams" + boost::lexical_cast<std::string>(j)] = Vector4f(0, 0, 9999, 0);
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	for (size_t i = 0; i < lineArr.size(); i++)
 | |
| 	{
 | |
| 
 | |
| 		size_t boxIndex = i / 16;
 | |
| 
 | |
| 		Vector2f startPoint = lineArr[i].first;
 | |
| 		Vector2f endPoint = lineArr[i].second;
 | |
| 
 | |
| 
 | |
| 		auto& pair = lineRenderPairArr[boxIndex];
 | |
| 
 | |
| 
 | |
| 
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0)- BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0) - BUFFER_DISTANCE, endPoint(1)+ BUFFER_DISTANCE, 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0) + BUFFER_DISTANCE, endPoint(1) + BUFFER_DISTANCE, 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0) - BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0) + BUFFER_DISTANCE, endPoint(1) + BUFFER_DISTANCE, 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0) + BUFFER_DISTANCE, startPoint(1) - BUFFER_DISTANCE, 0));
 | |
| 
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 
 | |
| 		Vector2f lineVec = endPoint - startPoint;
 | |
| 		Vector2f lineNormVec = { -lineVec(1), lineVec(0) };
 | |
| 
 | |
| 		lineNormVec.normalize();
 | |
| 
 | |
| 		//float distance = abs(normVec.x*(position.x - startPoint.x) + normVec.y*(position.y - startPoint.y));
 | |
| 		//distance = a*x + b*y + c, here we calculate a, b,c and pass them to shader
 | |
| 		float a = lineNormVec(0);
 | |
| 		float b = lineNormVec(1);
 | |
| 		float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1));
 | |
| 
 | |
| 
 | |
| 		pair.first.Vec4Map["lineParams" + boost::lexical_cast<std::string>(i % 16)] = Vector4f(a, b, c, 0);
 | |
| 
 | |
| 	}
 | |
| 
 | |
| 	for (auto& pair : lineRenderPairArr)
 | |
| 	{
 | |
| 		pair.second.RefreshBuffer();
 | |
| 	}
 | |
| 
 | |
| 
 | |
| 	return lineRenderPairArr;
 | |
| }
 | |
| 
 | |
| 
 | |
| #else
 | |
| 
 | |
| 
 | |
| std::vector<TRenderPair> linesToRenderBoxes(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
 | |
| {
 | |
| 	std::vector<TRenderPair> lineRenderPairArr;
 | |
| 
 | |
| 	lineRenderPairArr.resize(lineArr.size());
 | |
| 
 | |
| 	for (size_t i = 0; i < lineArr.size(); i++)
 | |
| 	{
 | |
| 
 | |
| 		Vector2f startPoint = lineArr[i].first;
 | |
| 		Vector2f endPoint = lineArr[i].second;
 | |
| 
 | |
| 
 | |
| 		auto& pair = lineRenderPairArr[i];
 | |
| 
 | |
| 		Vector2f renderAreaStart{ std::min<float>(startPoint(0), endPoint(0)) - BUFFER_DISTANCE, std::min<float>(startPoint(1), endPoint(1)) - BUFFER_DISTANCE };
 | |
| 
 | |
| 		Vector2f renderAreEnd{ std::max<float>(startPoint(0), endPoint(0)) + BUFFER_DISTANCE, std::max<float>(startPoint(1), endPoint(1)) + BUFFER_DISTANCE };
 | |
| 
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreaStart(0), renderAreaStart(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreaStart(0), renderAreEnd(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreEnd(0), renderAreEnd(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreaStart(0), renderAreaStart(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreEnd(0), renderAreEnd(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(renderAreEnd(0), renderAreaStart(1), 0));
 | |
| 
 | |
| 
 | |
| 		/*
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), endPoint(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(startPoint(0), startPoint(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), endPoint(1), 0));
 | |
| 		pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(endPoint(0), startPoint(1), 0));
 | |
| 		*/
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 		pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 0, 0, 1));
 | |
| 
 | |
| 		Vector2f lineVec = endPoint - startPoint;
 | |
| 		Vector2f lineNormVec = { -lineVec(1), lineVec(0) };
 | |
| 
 | |
| 		lineNormVec.normalize();
 | |
| 
 | |
| 		//float distance = abs(normVec.x*(position.x - startPoint.x) + normVec.y*(position.y - startPoint.y));
 | |
| 		//distance = a*x + b*y + c, here we calculate a, b,c and pass them to shader
 | |
| 		float a = lineNormVec(0);
 | |
| 		float b = lineNormVec(1);
 | |
| 		float c = -(lineNormVec(0)*startPoint(0) + lineNormVec(1)*startPoint(1));
 | |
| 
 | |
| 		pair.first.Vec4Map["lineParams"] = Vector4f(a, b, c, 0);
 | |
| 		pair.first.Vec4Map["startPointEndPoint"] = Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1));
 | |
| 
 | |
| 		pair.second.RefreshBuffer();
 | |
| 		
 | |
| 	}
 | |
| 
 | |
| 	
 | |
| 
 | |
| 	return lineRenderPairArr;
 | |
| }
 | |
| 
 | |
| 
 | |
| #endif
 | |
| 
 | |
| 
 | |
| void TMyApplication::InnerInit()
 | |
| {
 | |
| 
 | |
|     Application = this;
 | |
|     
 | |
| #ifdef TARGET_WIN32
 | |
| #ifdef NDEBUG
 | |
| 	ST::PathToResources = "resources/";
 | |
| #else
 | |
| 	ST::PathToResources = "../../../assets/";
 | |
| #endif
 | |
| #endif
 | |
|     
 | |
| #ifdef TARGET_IOS
 | |
|     ST::PathToResources = "assets/";
 | |
| #endif
 | |
| 
 | |
|     if (Console != NULL)
 | |
|     {
 | |
|         *Console<<"APP INIT\n";
 | |
|     }
 | |
|     srand (static_cast<size_t>(time(NULL)));
 | |
| 
 | |
| 	ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt");
 | |
| 	ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt");
 | |
| 
 | |
| #ifdef USE_SHADER_X16
 | |
| 	ResourceManager->ShaderManager.AddShader("AntialiasingShader", "ms_vertex.txt", "ms_fragment_x16.txt");
 | |
| #else
 | |
| 	ResourceManager->ShaderManager.AddShader("AntialiasingShader", "ms_vertex.txt", "ms_fragment.txt");
 | |
| #endif
 | |
| 	
 | |
| 	
 | |
| 
 | |
| 
 | |
| 	Renderer->PushShader("DefaultShader");
 | |
| 
 | |
| 	ResourceManager->TexList.AddTexture("console_bkg.bmp");
 | |
| 
 | |
| 	
 | |
| 	std::vector<std::pair<Vector2f, Vector2f>> lineArr;
 | |
| 
 | |
| 	for (int i = 0; i < 10; i++)
 | |
| 	{
 | |
| 		lineArr.push_back({ {50.f + i * 20.f, 50.f}, { 60.f + i * 20.f, 440.f } });
 | |
| 
 | |
| 		lineArr.push_back({ { 60.f + i * 20.f, 440.f },{ 70.f + i * 20.f, 50.f } });
 | |
| 	};
 | |
| 
 | |
| 
 | |
| #ifdef USE_SHADER_X16
 | |
| 	lineRenderPairArr = linesToRenderBoxesX16(lineArr);
 | |
| 
 | |
| #else
 | |
| 	lineRenderPairArr = linesToRenderBoxes(lineArr);
 | |
| 
 | |
| #endif
 | |
| 	
 | |
| 
 | |
| 	Renderer->SetOrthoProjection();
 | |
| 
 | |
| 	Renderer->SetFullScreenViewport();
 | |
| 	
 | |
| 	Inited = true;
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| }
 | |
| 
 | |
| void TMyApplication::InnerDeinit()
 | |
| {
 | |
|     Inited = false;
 | |
|     Loaded = false;
 | |
|     if (Console != NULL)
 | |
|     {
 | |
|         *Console<<"APP DEINIT\n";
 | |
|     }
 | |
| }
 | |
| 
 | |
| void TMyApplication::InnerOnTapDown(Vector2f p)
 | |
| {
 | |
| 
 | |
| }
 | |
| 
 | |
| void TMyApplication::InnerOnTapUp(Vector2f p)
 | |
| {
 | |
| 
 | |
| }
 | |
| 
 | |
| void TMyApplication::InnerOnTapUpAfterMove(Vector2f p)
 | |
| {
 | |
| 
 | |
| }
 | |
| 
 | |
| void TMyApplication::InnerOnMove(Vector2f p, Vector2f shift)
 | |
| {
 | |
| 
 | |
| }
 | |
| 	
 | |
| void TMyApplication::OnFling(Vector2f v) 
 | |
| {
 | |
| }
 | |
| 
 | |
| 	
 | |
| void TMyApplication::InnerDraw()
 | |
| {
 | |
| 
 | |
| 	//Render the frame
 | |
| 	Renderer->PushShader("AntialiasingShader");
 | |
| 
 | |
| 	Renderer->SetProjectionMatrix(512.f, 512.f);
 | |
| 
 | |
| 	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
 | |
| 	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
 | |
| 	CheckGlError("");
 | |
| 
 | |
| 	for (auto& pair : lineRenderPairArr)
 | |
| 	{
 | |
| 		TRenderParamsSetter params(pair.first);
 | |
| 		Renderer->DrawTriangleList(pair.second);
 | |
| 	}
 | |
| 
 | |
| 	Renderer->PopShader();
 | |
| 
 | |
| 	CheckGlError("");
 | |
| 
 | |
| 	
 | |
| }
 | |
| 
 | |
| 
 | |
| void TMyApplication::InnerUpdate(size_t dt)
 | |
| {
 | |
| 
 | |
| }
 | |
| 
 | |
| bool TMyApplication::IsLoaded()
 | |
| {
 | |
|     return Loaded;
 | |
| }
 | |
| 
 | |
| bool TMyApplication::IsInited()
 | |
| {
 | |
|     return Inited;
 | |
| }
 |