double-hit-balls/game/main_code.cpp

229 lines
6.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;
TRenderPair linesToRenderBoxes(const std::vector<std::pair<Vector2f, Vector2f>>& lineArr)
{
TRenderPair pair;
for (size_t i = 0; i < lineArr.size(); i++)
{
Vector2f startPoint = lineArr[i].first;
Vector2f endPoint = lineArr[i].second;
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.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.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec3CoordArr["vLineParams"].push_back(Vector3f(a, b, c));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
pair.second.Data.Vec4CoordArr["vStartPointEndPoint"].push_back(Vector4f(startPoint(0), startPoint(1), endPoint(0), endPoint(1)));
}
pair.second.RefreshBuffer();
return pair;
}
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");
Renderer->PushShader("AntialiasingShader");
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 } });
};
lineRenderPair = linesToRenderBoxes(lineArr);
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("");
EnableVertexAttribArray("vLineParams");
EnableVertexAttribArray("vStartPointEndPoint");
TRenderParamsSetter params(lineRenderPair.first);
Renderer->DrawTriangleList(lineRenderPair.second);
DisableVertexAttribArray("vStartPointEndPoint");
DisableVertexAttribArray("vLineParams");
Renderer->PopShader();
CheckGlError("");
}
void TMyApplication::InnerUpdate(size_t dt)
{
}
bool TMyApplication::IsLoaded()
{
return Loaded;
}
bool TMyApplication::IsInited()
{
return Inited;
}