double-hit-balls/game/main_code.cpp

353 lines
10 KiB
C++
Raw Permalink Normal View History

2017-01-10 12:43:06 +00:00
#include "main_code.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "include/Engine.h"
#include "main_code.h"
2017-11-14 17:04:04 +00:00
TMyApplication* Application;
2017-01-10 12:43:06 +00:00
2017-11-14 17:04:04 +00:00
2018-01-05 21:51:05 +00:00
#define AREATEX_WIDTH 160
#define AREATEX_HEIGHT 560
#define SEARCHTEX_WIDTH 66
#define SEARCHTEX_HEIGHT 33
GLuint area_tex;
GLuint search_tex;
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerInit()
2017-01-10 12:43:06 +00:00
{
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";
}
2017-11-14 17:04:04 +00:00
srand (static_cast<size_t>(time(NULL)));
2017-01-10 12:43:06 +00:00
ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt");
ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt");
2017-12-02 10:25:02 +00:00
ResourceManager->ShaderManager.AddShader("ColorShader", "color_vertex.txt", "color_fragment.txt");
2018-01-05 21:51:05 +00:00
ResourceManager->ShaderManager.AddShader("SmaaEdge", "smaa/edge_vertex.txt", "smaa/edge_fragment.txt");
ResourceManager->ShaderManager.AddShader("SmaaBlend", "smaa/blend_vertex.txt", "smaa/blend_fragment.txt");
ResourceManager->ShaderManager.AddShader("SmaaNeighborhood", "smaa/neighborhood_vertex.txt", "smaa/neighborhood_fragment.txt");
2017-01-10 12:43:06 +00:00
Renderer->PushShader("DefaultShader");
2017-11-14 17:04:04 +00:00
2017-01-10 12:43:06 +00:00
ResourceManager->TexList.AddTexture("console_bkg.bmp");
2017-11-14 17:04:04 +00:00
2018-01-05 21:51:05 +00:00
ResourceManager->FrameManager.AddFrameRenderBuffer("AlbedoBuffer", 512, 512);
ResourceManager->FrameManager.AddFrameRenderBuffer("EdgeBuffer", 512, 512);
ResourceManager->FrameManager.AddFrameRenderBuffer("BlendBuffer", 512, 512);
2017-12-04 11:14:14 +00:00
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
pair.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 0));
2018-01-05 21:51:05 +00:00
//Choose colored or black and white -- Vladislav Khorev vladislav.khorev@fishrungames.com
#if 1
2017-12-04 11:14:14 +00:00
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(0, 1, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 1, 0, 1));
2018-01-05 21:51:05 +00:00
#else
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 1, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 1, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(1, 1, 1, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 0, 1));
pair.second.Data.Vec4CoordArr[CONST_STRING_COLOR_ATTRIB].push_back(Vector4f(0, 0, 0, 1));
2017-12-04 11:14:14 +00:00
2018-01-05 21:51:05 +00:00
#endif
2017-12-04 11:14:14 +00:00
pair.first.ShaderName = "ColorShader";
pair.second.RefreshBuffer();
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 512, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(512, 0, 0));
rect.second.Data.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].push_back(Vector3f(0, 0, 0));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 1));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 1));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 1));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(1, 0));
rect.second.Data.Vec2CoordArr[CONST_STRING_TEXCOORD_ATTRIB].push_back(Vector2f(0, 0));
rect.second.RefreshBuffer();
2017-01-10 12:43:06 +00:00
Renderer->SetOrthoProjection();
Renderer->SetFullScreenViewport();
2018-01-05 21:51:05 +00:00
glDisable(GL_BLEND); //Don't forget to disable blending or whole thing will not work!!! -- Vladislav Khorev vladislav.khorev@fishrungames.com
2017-12-02 10:25:02 +00:00
2017-12-04 11:14:14 +00:00
Inited = true;
2017-12-02 10:25:02 +00:00
2018-01-05 21:51:05 +00:00
//This is stuff from original https://github.com/scrawl/smaa-opengl
//We must have these textures to be used for pattern matching or something -- Vladislav Khorev vladislav.khorev@fishrungames.com
unsigned char* buffer = 0;
FILE* f = 0;
buffer = new unsigned char[1024 * 1024];
f = fopen((ST::PathToResources + "smaa/smaa_area.raw").c_str(), "rb"); //rb stands for "read binary file"
if (!f)
{
std::cerr << "Couldn't open smaa_area.raw.\n";
}
fread(buffer, AREATEX_WIDTH * AREATEX_HEIGHT * 2, 1, f);
fclose(f);
f = 0;
glGenTextures(1, &area_tex);
glBindTexture(GL_TEXTURE_2D, area_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, (GLsizei)AREATEX_WIDTH, (GLsizei)AREATEX_HEIGHT, 0, GL_RG, GL_UNSIGNED_BYTE, buffer);
f = fopen((ST::PathToResources + "smaa/smaa_search.raw").c_str(), "rb");
if (!f)
{
std::cerr << "Couldn't open smaa_search.raw.\n";
}
fread(buffer, SEARCHTEX_WIDTH * SEARCHTEX_HEIGHT, 1, f);
fclose(f);
f = 0;
glGenTextures(1, &search_tex);
glBindTexture(GL_TEXTURE_2D, search_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, (GLsizei)SEARCHTEX_WIDTH, (GLsizei)SEARCHTEX_HEIGHT, 0, GL_RED, GL_UNSIGNED_BYTE, buffer);
delete[] buffer;
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerDeinit()
2017-01-10 12:43:06 +00:00
{
Inited = false;
Loaded = false;
if (Console != NULL)
{
*Console<<"APP DEINIT\n";
}
2018-01-05 21:51:05 +00:00
glDeleteTextures(1, &search_tex);
glDeleteTextures(1, &area_tex);
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnTapDown(Vector2f p)
{
2017-12-04 11:14:14 +00:00
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnTapUp(Vector2f p)
{
2017-12-04 11:14:14 +00:00
2017-01-10 12:43:06 +00:00
}
2017-02-26 23:07:37 +00:00
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnTapUpAfterMove(Vector2f p)
2017-02-26 23:07:37 +00:00
{
2017-12-04 11:14:14 +00:00
2017-02-26 23:07:37 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerOnMove(Vector2f p, Vector2f shift)
2017-01-10 12:43:06 +00:00
{
2017-12-04 11:14:14 +00:00
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::OnFling(Vector2f v)
2017-01-10 12:43:06 +00:00
{
}
2017-12-04 11:14:14 +00:00
void TMyApplication::InnerDraw()
2017-01-10 12:43:06 +00:00
{
2018-01-05 21:51:05 +00:00
//Render the frame
Renderer->SwitchToFrameBuffer("AlbedoBuffer");
2017-12-04 11:14:14 +00:00
Renderer->SetProjectionMatrix(512.f, 512.f);
2018-01-05 21:51:05 +00:00
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
2017-12-04 11:14:14 +00:00
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
CheckGlError("");
2017-01-10 12:43:06 +00:00
2017-12-04 11:14:14 +00:00
{
2018-01-05 21:51:05 +00:00
//Render here all objects that you want to have -- Vladislav Khorev vladislav.khorev@fishrungames.com
2017-12-04 11:14:14 +00:00
TRenderParamsSetter params(pair.first);
Renderer->DrawTriangleList(pair.second);
}
2017-01-10 12:43:06 +00:00
2017-12-04 11:14:14 +00:00
CheckGlError("");
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
//-------------------------
//First pass - edge detection
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
Renderer->PushShader("SmaaEdge");
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
Renderer->SwitchToFrameBuffer("EdgeBuffer");
Renderer->SetProjectionMatrix(512.f, 512.f);
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
RenderUniform1i("albedo_tex", 0);
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("AlbedoBuffer"));
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
Renderer->DrawTriangleList(rect.second);
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
Renderer->PopShader();
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
CheckGlError("");
//-------------------------
//Second pass - blend plus pattern matching
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
Renderer->PushShader("SmaaBlend");
Renderer->SwitchToFrameBuffer("BlendBuffer");
Renderer->SetProjectionMatrix(512.f, 512.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
RenderUniform1i("edge_tex", 0);
RenderUniform1i("area_tex", 1);
RenderUniform1i("search_tex", 2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("EdgeBuffer"));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, area_tex);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, search_tex);
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError("");
#if 1
//-------------------------
//Last pass - neightborhood
2017-12-04 11:14:14 +00:00
2018-01-05 21:51:05 +00:00
Renderer->PushShader("SmaaNeighborhood");
Renderer->SwitchToScreen();
Renderer->SetFullScreenViewport();
Renderer->SetProjectionMatrix(512.f, 512.f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
RenderUniform1i("albedo_tex", 0);
RenderUniform1i("blend_tex", 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("BlendBuffer"));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("AlbedoBuffer"));
2017-12-04 11:14:14 +00:00
Renderer->DrawTriangleList(rect.second);
2017-01-10 12:43:06 +00:00
2017-12-04 11:14:14 +00:00
Renderer->PopShader();
2017-01-10 12:43:06 +00:00
2017-12-04 11:14:14 +00:00
CheckGlError("");
2018-01-05 21:51:05 +00:00
#else
//... or just render intermediate buffers to see what is going on
Renderer->PushShader("DefaultShader");
Renderer->SwitchToScreen();
Renderer->SetFullScreenViewport();
Renderer->SetProjectionMatrix(512.f, 512.f);
2017-01-10 12:43:06 +00:00
2018-01-05 21:51:05 +00:00
glActiveTexture(GL_TEXTURE0);
//Choose any to render for debug:
//glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("AlbedoBuffer"));
//glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("EdgeBuffer"));
glBindTexture(GL_TEXTURE_2D, ResourceManager->FrameManager.GetFrameTexture("BlendBuffer"));
Renderer->DrawTriangleList(rect.second);
Renderer->PopShader();
CheckGlError("");
#endif
2017-01-10 12:43:06 +00:00
}
2017-11-14 17:04:04 +00:00
void TMyApplication::InnerUpdate(size_t dt)
2017-01-10 12:43:06 +00:00
{
}
2017-11-14 17:04:04 +00:00
bool TMyApplication::IsLoaded()
2017-01-10 12:43:06 +00:00
{
return Loaded;
}
2017-11-14 17:04:04 +00:00
bool TMyApplication::IsInited()
2017-01-10 12:43:06 +00:00
{
return Inited;
}