Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- N'gine Lib for C++
- Ejemplo del uso del Canvas (Programa)
- Proyecto iniciado el 1 de Febrero del 2016
- (cc) 2016 - 2020 by Cesar Rincon "NightFox"
- https://nightfoxandco.com
- contact@nightfoxandco.com
- Requiere N'gine 1.0.0-stable o superior
- Requiere GCC 7.3.0 MinGW (SEH) - 64-bits
- http://downloads.sourceforge.net/project/mingw-w64/
- Requiere SDL2 (2.0.12) - 64-bits
- http://www.libsdl.org/download-2.0.php
- Requiere SDL2_TTF (2.0.15) - 64-bits
- http://www.libsdl.org/download-2.0.php
- Requiere SFML (2.5.1) - 64-bits
- http://www.sfml-dev.org/
- Requiere LodePNG (20200306)
- (c) 2005 - 2020 by Lode Vandevenne
- http://lodev.org/lodepng/
- N'gine se distribuye bajo la licencia CREATIVE COMMONS
- "Attribution-NonCommercial 4.0 International"
- https://creativecommons.org/licenses/by-nc/4.0/
- You are free to:
- - Share
- copy and redistribute the material in any medium or format.
- - Adapt
- remix, transform, and build upon the material.
- The licensor cannot revoke these freedoms as long as you follow
- the license terms.
- Under the following terms:
- - Attribution
- You must give appropriate credit, provide a link to the license,
- and indicate if changes were made. You may do so in any reasonable
- manner, but not in any way that suggests the licensor endorses you
- or your use.
- - NonCommercial
- You may not use the material for commercial purposes.
- - No additional restrictions
- You may not apply legal terms or technological measures that
- legally restrict others from doing anything the license permits.
- ******************************************************************************/
- /*** Includes ***/
- // c++
- #include <cstdio>
- #include <iostream>
- #include <cmath>
- // Includes de la libreria
- #include <ngn.h>
- // Includes del programa
- #include "demo.h"
- /*** Constructor de la clase ***/
- Demo::Demo() {
- // Prepara los punteros
- bg = NULL; // Canvas del fondo
- // Vector de datos
- deep.clear();
- }
- /*** Destructor de la clase ***/
- Demo::~Demo() {
- // Elimina los canvas
- delete bg; bg = NULL; // Fondo
- // Vacia el vecto de datos
- deep.clear();
- }
- /*** Inicializa N'GINE ***/
- bool Demo::Awake() {
- // Inicializa la libreria
- if (!ngn->Init()) {
- std::cout << "Critical error, can't initialize n'gine." << std::endl;
- return false;
- }
- // Crea la ventana con la resolucion nativa
- if (!ngn->graphics->Init(WINDOW_TITLE, SCR_WIDTH, SCR_HEIGHT, NGN_SCR_WINDOW, BILINEAR_FILTER, VSYNC)) return false;
- ngn->graphics->Update();
- // visibilidad del cursor del raton
- ngn->graphics->ShowMouse(SHOW_MOUSE);
- // Contador de FPS activo?
- ngn->system->fps_counter = FPS_COUNTER;
- // Selecciona el modo grafico (ventana/full screen)
- #if defined (OS_WINDOWS)
- ngn->graphics->SetMode(SCR_MODE_WINDOWS);
- #elif defined (OS_LINUX)
- ngn->graphics->SetMode(SCR_MODE_LINUX);
- #endif
- ngn->graphics->Update();
- // Muestra la version de la libreria en la consola
- #if defined (MODE_DEBUG)
- std::cout << ngn->system->GetVersion() << std::endl;
- #endif
- // Inicializacion completada con exito
- return true;
- }
- /*** Al iniciar el programa ***/
- bool Demo::Start() {
- // Carga de archivos
- if (!Load()) return false;
- // Crea los elementos del programa
- Create();
- // Procesos correctos
- return true;
- }
- /*** Ejecucion del programa ***/
- int8_t Demo::Run() {
- // Control del loop
- int8_t loop = -1;
- while (loop < 0) {
- // Gestor de eventos de SDL y N'gine
- ngn->system->EventUpdate(); // Actualiza los eventos
- /***
- Bucle principal del programa
- ***/
- Logic(); // Logica del programa
- Render(); // Render de los elementos graficos
- // Actualiza el contenido de la pantalla
- ngn->graphics->Update();
- // Actualiza el sonido
- ngn->sound->Update();
- // Control del bucle principal
- if (ngn->system->quit) { // Si se pulsa la [X] de la ventana
- loop = 0;
- } else if (ngn->input->key_ESC->down || ngn->input->controller[0].button[10].down) { // Si se pulsa la tecla [ESC] O se pulsa el boton XBOX
- loop = 1;
- }
- }
- // Devuelve el resultado
- return loop;
- }
- /*** Carga de los archivos necesarios ***/
- bool Demo::Load() {
- // Archivos cargados con exito
- return true;
- }
- /*** Crea los elementos del programa ***/
- void Demo::Create() {
- // Crea el canvas del fondo
- bg = new NGN_Canvas(); // Pantalla completa
- // Calculos del tunel
- origin.top = 0;
- origin.bottom = (SCR_HEIGHT - 1);
- origin.left = 0;
- origin.right = (SCR_WIDTH - 1);
- w = (SCR_WIDTH / sq_size);
- h = (SCR_HEIGHT/ sq_size);
- tunnel_bg.top = (h / 2);
- tunnel_bg.bottom = (h / 2);
- tunnel_bg.left = (w / 2);
- tunnel_bg.right = (w / 2);
- // Crea un buffer de profundidad
- float z = 1.0f;
- do {
- z *= deep_step;
- if (z > z_size) continue;
- deep.push_back(z);
- //std::cout << z << std::endl;
- } while (z < z_size);
- red = 0;
- }
- /*** Logica del programa ***/
- void Demo::Logic() {
- // Borra el canvas
- bg->Cls();
- DrawTunnel();
- }
- /*** Render de los elementos graficos ***/
- void Demo::Render() {
- ngn->render->Canvas(bg);
- }
- /*** Dibuja el tunel ***/
- void Demo::DrawTunnel() {
- // Coordenadas del mouse
- int32_t mx = ngn->input->mouse.x;
- int32_t my = ngn->input->mouse.y;
- if (mx < w) mx = w;
- if (mx > ((int32_t)SCR_WIDTH - w)) mx = ((int32_t)SCR_WIDTH - w);
- if (my < h) my = h;
- if (my > ((int32_t)SCR_HEIGHT - h)) my = ((int32_t)SCR_HEIGHT - h);
- // Calculo del fondo del tunel
- int32_t x1 = (mx - tunnel_bg.left);
- int32_t y1 = (my - tunnel_bg.top);
- int32_t x2 = (mx + tunnel_bg.right);
- int32_t y2 = (my + tunnel_bg.bottom);
- // Calculo de las lineas de profundidad
- float _x1, _x2, _y1, _y2;
- _x1 = (float)std::abs(x1 - origin.left);
- _x2 = (float)std::abs(x2 - origin.right);
- _y1 = (float)std::abs(y1 - origin.top);
- _y2 = (float)std::abs(y2 - origin.bottom);
- int32_t lx1, ly1, lx2, ly2; // Vertices del marco actual
- int32_t ox1, oy1, ox2, oy2; // Vertices del marco anterior
- uint32_t r = 0, g = 0, b = 0; // Componentes del color
- uint32_t gray = 0; // Componente gris
- uint32_t color = 0; // Color
- ox1 = origin.left; // Define el primer marco fuera del punto de vista
- ox2 = origin.right;
- oy1 = origin.top;
- oy2 = origin.bottom;
- // Componente rojo
- r = std::round(std::sin(red) * (float)0x7F) + 0x7F;
- red += 0.05f;
- if (red > 6.28f) red -= 6.28f;
- // Segmentos
- for (int32_t i = ((int32_t)deep.size() - 1); i >= 0; i --) {
- // Calculo del color
- g = (std::ceil((deep[i] * (float)0xDF) / z_size) + 0x20);
- b = (0xFF - g);
- color = (r << 24) | (g << 16) | (b << 8) | 0xFF;
- // Calculos del marco actual
- lx1 = std::round((float)x1 - ((_x1 * deep[i]) / z_size));
- ly1 = std::round((float)y1 - ((_y1 * deep[i]) / z_size));
- lx2 = std::round((float)x2 + ((_x2 * deep[i]) / z_size));
- ly2 = std::round((float)y2 + ((_y2 * deep[i]) / z_size));
- // Dibuja las lineas
- DrawLines(ox1, oy1, ox2, oy2, lx1, ly1, lx2, ly2, color);
- // Guarda los valores para la siguiente linea
- ox1 = lx1;
- ox2 = lx2;
- oy1 = ly1;
- oy2 = ly2;
- }
- }
- void Demo::DrawLines(
- int32_t ox1, int32_t oy1,
- int32_t ox2, int32_t oy2,
- int32_t lx1, int32_t ly1,
- int32_t lx2, int32_t ly2,
- uint32_t color
- ) {
- // Vertices
- bg->Line(ox1, oy1, lx1, ly1, color);
- bg->Line(ox2, oy1, lx2, ly1, color);
- bg->Line(ox1, oy2, lx1, ly2, color);
- bg->Line(ox2, oy2, lx2, ly2, color);
- // Segmentos intermedios
- int32_t tx1 = 0, ty1 = 0, tx2 = 0, ty2 = 0;
- for (int i = 1; i < seg_size; i ++) {
- // Superior
- tx1 = std::round((((float)std::abs(ox1 - ox2) / (float)seg_size) * (float)i) + (float)ox1);
- ty1 = oy1;
- tx2 = std::round((((float)std::abs(lx1 - lx2) / (float)seg_size) * (float)i) + (float)lx1);
- ty2 = ly1;
- bg->Line(tx1, ty1, tx2, ty2, color);
- // Inferior
- tx1 = std::round((((float)std::abs(ox1 - ox2) / (float)seg_size) * (float)i) + (float)ox1);
- ty1 = oy2;
- tx2 = std::round((((float)std::abs(lx1 - lx2) / (float)seg_size) * (float)i) + (float)lx1);
- ty2 = ly2;
- bg->Line(tx1, ty1, tx2, ty2, color);
- // Izquierda
- tx1 = ox1;
- ty1 = std::round((((float)std::abs(oy1 - oy2) / (float)seg_size) * (float)i) + (float)oy1);
- tx2 = lx1;
- ty2 = std::round((((float)std::abs(ly1 - ly2) / (float)seg_size) * (float)i) + (float)ly1);
- bg->Line(tx1, ty1, tx2, ty2, color);
- // Derecha
- tx1 = ox2;
- ty1 = std::round((((float)std::abs(oy1 - oy2) / (float)seg_size) * (float)i) + (float)oy1);
- tx2 = lx2;
- ty2 = std::round((((float)std::abs(ly1 - ly2) / (float)seg_size) * (float)i) + (float)ly1);
- bg->Line(tx1, ty1, tx2, ty2, color);
- }
- // Marco
- bg->Box(lx1, ly1, lx2, ly2, color);
- }
Add Comment
Please, Sign In to add comment