libgui/source/video/CursorDrawer.cpp

85 lines
2.9 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2016,2017 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2019-08-14 23:24:55 +02:00
#include <gui/video/shaders/ColorShader.h>
#include <gui/video/shaders/FXAAShader.h>
#include <gui/video/shaders/Shader3D.h>
#include <gui/video/shaders/ShaderFractalColor.h>
#include <gui/video/shaders/Texture2DShader.h>
#include <gui/video/CursorDrawer.h>
2017-10-29 10:28:14 +01:00
CursorDrawer *CursorDrawer::instance = NULL;
2018-06-21 20:44:58 +02:00
CursorDrawer::CursorDrawer() {
2017-10-29 10:28:14 +01:00
init_colorVtxs();
}
2018-06-21 20:44:58 +02:00
CursorDrawer::~CursorDrawer() {
2017-10-29 10:28:14 +01:00
//! destroy shaders
ColorShader::destroyInstance();
FXAAShader::destroyInstance();
Shader3D::destroyInstance();
ShaderFractalColor::destroyInstance();
Texture2DShader::destroyInstance();
2020-08-13 12:38:07 +02:00
if (this->colorVtxs) {
2017-10-29 10:28:14 +01:00
free(this->colorVtxs);
this->colorVtxs = NULL;
}
}
2018-06-21 20:44:58 +02:00
void CursorDrawer::init_colorVtxs() {
2020-08-13 12:38:07 +02:00
if (!this->colorVtxs) {
this->colorVtxs = (uint8_t *) memalign(0x40, sizeof(uint8_t) * 16);
2020-08-13 12:58:19 +02:00
if (this->colorVtxs == NULL) { return; }
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
memset(this->colorVtxs, 0xFF, 16 * sizeof(uint8_t));
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, this->colorVtxs, 16 * sizeof(uint8_t));
2017-10-29 10:28:14 +01:00
}
// Could be improved. It be more generic.
2020-08-13 12:38:07 +02:00
void CursorDrawer::draw_Cursor(float x, float y) {
if (this->colorVtxs == NULL) {
2017-10-29 10:28:14 +01:00
init_colorVtxs();
return;
}
2020-08-13 12:38:07 +02:00
float widthScaleFactor = 1.0f / (float) 1280;
float heightScaleFactor = 1.0f / (float) 720;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
int32_t width = 20;
2017-10-29 10:28:14 +01:00
glm::vec3 positionOffsets = glm::vec3(0.0f);
2020-08-13 12:38:07 +02:00
positionOffsets[0] = (x - ((1280) / 2) + (width / 2)) * widthScaleFactor * 2.0f;
positionOffsets[1] = -(y - ((720) / 2) + (width / 2)) * heightScaleFactor * 2.0f;
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
glm::vec3 scale(width * widthScaleFactor, width * heightScaleFactor, 1.0f);
2017-10-29 10:28:14 +01:00
ColorShader::instance()->setShaders();
ColorShader::instance()->setAttributeBuffer(this->colorVtxs, NULL, 4);
ColorShader::instance()->setAngle(0);
ColorShader::instance()->setOffset(positionOffsets);
ColorShader::instance()->setScale(scale);
ColorShader::instance()->setColorIntensity(glm::vec4(1.0f));
2018-06-21 20:44:58 +02:00
ColorShader::instance()->draw(GX2_PRIMITIVE_MODE_QUADS, 4);
2017-10-29 10:28:14 +01:00
}