NightFox

Ejemplo del uso del Canvas

Nov 4th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.71 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.     N'gine Lib for C++
  4.     Ejemplo del uso del Canvas (Programa)
  5.  
  6.     Proyecto iniciado el 1 de Febrero del 2016
  7.     (cc) 2016 - 2020 by Cesar Rincon "NightFox"
  8.     https://nightfoxandco.com
  9.     contact@nightfoxandco.com
  10.  
  11.     Requiere N'gine 1.0.0-stable o superior
  12.  
  13.     Requiere GCC 7.3.0 MinGW (SEH) - 64-bits
  14.     http://downloads.sourceforge.net/project/mingw-w64/
  15.  
  16.     Requiere SDL2 (2.0.12) - 64-bits
  17.     http://www.libsdl.org/download-2.0.php
  18.  
  19.     Requiere SDL2_TTF (2.0.15) - 64-bits
  20.     http://www.libsdl.org/download-2.0.php
  21.  
  22.     Requiere SFML (2.5.1) - 64-bits
  23.     http://www.sfml-dev.org/
  24.  
  25.     Requiere LodePNG (20200306)
  26.     (c) 2005 - 2020 by Lode Vandevenne
  27.     http://lodev.org/lodepng/
  28.  
  29.  
  30.     N'gine se distribuye bajo la licencia CREATIVE COMMONS
  31.     "Attribution-NonCommercial 4.0 International"
  32.     https://creativecommons.org/licenses/by-nc/4.0/
  33.  
  34.     You are free to:
  35.  
  36.         - Share
  37.         copy and redistribute the material in any medium or format.
  38.         - Adapt
  39.         remix, transform, and build upon the material.
  40.  
  41.         The licensor cannot revoke these freedoms as long as you follow
  42.         the license terms.
  43.  
  44.     Under the following terms:
  45.  
  46.         - Attribution
  47.         You must give appropriate credit, provide a link to the license,
  48.         and indicate if changes were made. You may do so in any reasonable
  49.         manner, but not in any way that suggests the licensor endorses you
  50.         or your use.
  51.  
  52.         - NonCommercial
  53.         You may not use the material for commercial purposes.
  54.  
  55.         - No additional restrictions
  56.         You may not apply legal terms or technological measures that
  57.         legally restrict others from doing anything the license permits.
  58.  
  59. ******************************************************************************/
  60.  
  61.  
  62.  
  63. /*** Includes ***/
  64. // c++
  65. #include <cstdio>
  66. #include <iostream>
  67. #include <cmath>
  68. // Includes de la libreria
  69. #include <ngn.h>
  70. // Includes del programa
  71. #include "demo.h"
  72.  
  73.  
  74.  
  75. /*** Constructor de la clase ***/
  76. Demo::Demo() {
  77.  
  78.     // Prepara los punteros
  79.     bg = NULL;      // Canvas del fondo
  80.  
  81.     // Vector de datos
  82.     deep.clear();
  83.  
  84. }
  85.  
  86.  
  87.  
  88. /*** Destructor de la clase ***/
  89. Demo::~Demo() {
  90.  
  91.     // Elimina los canvas
  92.     delete bg; bg = NULL;       // Fondo
  93.  
  94.     // Vacia el vecto de datos
  95.     deep.clear();
  96.  
  97. }
  98.  
  99.  
  100.  
  101. /*** Inicializa N'GINE ***/
  102. bool Demo::Awake() {
  103.  
  104.     // Inicializa la libreria
  105.     if (!ngn->Init()) {
  106.         std::cout << "Critical error, can't initialize n'gine." << std::endl;
  107.         return false;
  108.     }
  109.  
  110.     // Crea la ventana con la resolucion nativa
  111.     if (!ngn->graphics->Init(WINDOW_TITLE, SCR_WIDTH, SCR_HEIGHT, NGN_SCR_WINDOW, BILINEAR_FILTER, VSYNC)) return false;
  112.     ngn->graphics->Update();
  113.  
  114.     // visibilidad del cursor del raton
  115.     ngn->graphics->ShowMouse(SHOW_MOUSE);
  116.  
  117.     // Contador de FPS activo?
  118.     ngn->system->fps_counter = FPS_COUNTER;
  119.  
  120.     // Selecciona el modo grafico (ventana/full screen)
  121.     #if defined (OS_WINDOWS)
  122.         ngn->graphics->SetMode(SCR_MODE_WINDOWS);
  123.     #elif defined (OS_LINUX)
  124.         ngn->graphics->SetMode(SCR_MODE_LINUX);
  125.     #endif
  126.     ngn->graphics->Update();
  127.  
  128.     // Muestra la version de la libreria en la consola
  129.     #if defined (MODE_DEBUG)
  130.         std::cout << ngn->system->GetVersion() << std::endl;
  131.     #endif
  132.  
  133.     // Inicializacion completada con exito
  134.     return true;
  135.  
  136. }
  137.  
  138.  
  139.  
  140. /*** Al iniciar el programa ***/
  141. bool Demo::Start() {
  142.  
  143.     // Carga de archivos
  144.     if (!Load()) return false;
  145.  
  146.     // Crea los elementos del programa
  147.     Create();
  148.  
  149.     // Procesos correctos
  150.     return true;
  151.  
  152. }
  153.  
  154.  
  155.  
  156. /*** Ejecucion del programa ***/
  157. int8_t Demo::Run() {
  158.  
  159.     // Control del loop
  160.     int8_t loop = -1;
  161.  
  162.     while (loop < 0) {
  163.  
  164.         // Gestor de eventos de SDL y N'gine
  165.         ngn->system->EventUpdate();            // Actualiza los eventos
  166.  
  167.  
  168.         /***
  169.         Bucle principal del programa
  170.         ***/
  171.         Logic();        // Logica del programa
  172.         Render();       // Render de los elementos graficos
  173.  
  174.  
  175.         // Actualiza el contenido de la pantalla
  176.         ngn->graphics->Update();
  177.         // Actualiza el sonido
  178.         ngn->sound->Update();
  179.  
  180.         // Control del bucle principal
  181.         if (ngn->system->quit) {    // Si se pulsa la [X] de la ventana
  182.             loop = 0;
  183.         } 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
  184.             loop = 1;
  185.         }
  186.  
  187.     }
  188.  
  189.     // Devuelve el resultado
  190.     return loop;
  191.  
  192. }
  193.  
  194.  
  195.  
  196. /*** Carga de los archivos necesarios ***/
  197. bool Demo::Load() {
  198.  
  199.  
  200.     // Archivos cargados con exito
  201.     return true;
  202.  
  203. }
  204.  
  205.  
  206.  
  207. /*** Crea los elementos del programa ***/
  208. void Demo::Create() {
  209.  
  210.     // Crea el canvas del fondo
  211.     bg = new NGN_Canvas();      // Pantalla completa
  212.  
  213.     // Calculos del tunel
  214.     origin.top = 0;
  215.     origin.bottom = (SCR_HEIGHT - 1);
  216.     origin.left = 0;
  217.     origin.right = (SCR_WIDTH - 1);
  218.     w = (SCR_WIDTH / sq_size);
  219.     h = (SCR_HEIGHT/ sq_size);
  220.     tunnel_bg.top = (h / 2);
  221.     tunnel_bg.bottom = (h / 2);
  222.     tunnel_bg.left = (w / 2);
  223.     tunnel_bg.right = (w / 2);
  224.  
  225.     // Crea un buffer de profundidad
  226.     float z = 1.0f;
  227.     do {
  228.         z *= deep_step;
  229.         if (z > z_size) continue;
  230.         deep.push_back(z);
  231.         //std::cout << z << std::endl;
  232.     } while (z < z_size);
  233.  
  234.     red = 0;
  235.  
  236. }
  237.  
  238.  
  239.  
  240. /*** Logica del programa ***/
  241. void Demo::Logic() {
  242.  
  243.     // Borra el canvas
  244.     bg->Cls();
  245.  
  246.     DrawTunnel();
  247.  
  248. }
  249.  
  250.  
  251.  
  252. /*** Render de los elementos graficos ***/
  253. void Demo::Render() {
  254.  
  255.     ngn->render->Canvas(bg);
  256.  
  257. }
  258.  
  259.  
  260.  
  261. /*** Dibuja el tunel ***/
  262. void Demo::DrawTunnel() {
  263.  
  264.     // Coordenadas del mouse
  265.     int32_t mx = ngn->input->mouse.x;
  266.     int32_t my = ngn->input->mouse.y;
  267.     if (mx < w) mx = w;
  268.     if (mx > ((int32_t)SCR_WIDTH - w)) mx = ((int32_t)SCR_WIDTH - w);
  269.     if (my < h) my = h;
  270.     if (my > ((int32_t)SCR_HEIGHT - h)) my = ((int32_t)SCR_HEIGHT - h);
  271.  
  272.     // Calculo del fondo del tunel
  273.     int32_t x1 = (mx - tunnel_bg.left);
  274.     int32_t y1 = (my - tunnel_bg.top);
  275.     int32_t x2 = (mx + tunnel_bg.right);
  276.     int32_t y2 = (my + tunnel_bg.bottom);
  277.  
  278.     // Calculo de las lineas de profundidad
  279.     float _x1, _x2, _y1, _y2;
  280.     _x1 = (float)std::abs(x1 - origin.left);
  281.     _x2 = (float)std::abs(x2 - origin.right);
  282.     _y1 = (float)std::abs(y1 - origin.top);
  283.     _y2 = (float)std::abs(y2 - origin.bottom);
  284.  
  285.     int32_t lx1, ly1, lx2, ly2;         // Vertices del marco actual
  286.     int32_t ox1, oy1, ox2, oy2;         // Vertices del marco anterior
  287.     uint32_t r = 0, g = 0, b = 0;       // Componentes del color
  288.     uint32_t gray = 0;                  // Componente gris
  289.     uint32_t color = 0;                 // Color
  290.  
  291.     ox1 = origin.left;                  // Define el primer marco fuera del punto de vista
  292.     ox2 = origin.right;
  293.     oy1 = origin.top;
  294.     oy2 = origin.bottom;
  295.  
  296.     // Componente rojo
  297.     r = std::round(std::sin(red) * (float)0x7F) + 0x7F;
  298.     red += 0.05f;
  299.     if (red > 6.28f) red -= 6.28f;
  300.     // Segmentos
  301.     for (int32_t i = ((int32_t)deep.size() - 1); i >= 0; i --) {
  302.         // Calculo del color
  303.         g = (std::ceil((deep[i] * (float)0xDF) / z_size) + 0x20);
  304.         b = (0xFF - g);
  305.         color = (r << 24) | (g << 16) | (b << 8) | 0xFF;
  306.         // Calculos del marco actual
  307.         lx1 = std::round((float)x1 - ((_x1 * deep[i]) / z_size));
  308.         ly1 = std::round((float)y1 - ((_y1 * deep[i]) / z_size));
  309.         lx2 = std::round((float)x2 + ((_x2 * deep[i]) / z_size));
  310.         ly2 = std::round((float)y2 + ((_y2 * deep[i]) / z_size));
  311.         // Dibuja las lineas
  312.         DrawLines(ox1, oy1, ox2, oy2, lx1, ly1, lx2, ly2, color);
  313.         // Guarda los valores para la siguiente linea
  314.         ox1 = lx1;
  315.         ox2 = lx2;
  316.         oy1 = ly1;
  317.         oy2 = ly2;
  318.     }
  319.  
  320. }
  321.  
  322.  
  323.  
  324. void Demo::DrawLines(
  325.     int32_t ox1, int32_t oy1,
  326.     int32_t ox2, int32_t oy2,
  327.     int32_t lx1, int32_t ly1,
  328.     int32_t lx2, int32_t ly2,
  329.     uint32_t color
  330. ) {
  331.     // Vertices
  332.     bg->Line(ox1, oy1, lx1, ly1, color);
  333.     bg->Line(ox2, oy1, lx2, ly1, color);
  334.     bg->Line(ox1, oy2, lx1, ly2, color);
  335.     bg->Line(ox2, oy2, lx2, ly2, color);
  336.     // Segmentos intermedios
  337.     int32_t tx1 = 0, ty1 = 0, tx2 = 0, ty2 = 0;
  338.     for (int i = 1; i < seg_size; i ++) {
  339.         // Superior
  340.         tx1 = std::round((((float)std::abs(ox1 - ox2) / (float)seg_size) * (float)i) + (float)ox1);
  341.         ty1 = oy1;
  342.         tx2 = std::round((((float)std::abs(lx1 - lx2) / (float)seg_size) * (float)i) + (float)lx1);
  343.         ty2 = ly1;
  344.         bg->Line(tx1, ty1, tx2, ty2, color);
  345.         // Inferior
  346.         tx1 = std::round((((float)std::abs(ox1 - ox2) / (float)seg_size) * (float)i) + (float)ox1);
  347.         ty1 = oy2;
  348.         tx2 = std::round((((float)std::abs(lx1 - lx2) / (float)seg_size) * (float)i) + (float)lx1);
  349.         ty2 = ly2;
  350.         bg->Line(tx1, ty1, tx2, ty2, color);
  351.         // Izquierda
  352.         tx1 = ox1;
  353.         ty1 = std::round((((float)std::abs(oy1 - oy2) / (float)seg_size) * (float)i) + (float)oy1);
  354.         tx2 = lx1;
  355.         ty2 = std::round((((float)std::abs(ly1 - ly2) / (float)seg_size) * (float)i) + (float)ly1);
  356.         bg->Line(tx1, ty1, tx2, ty2, color);
  357.         // Derecha
  358.         tx1 = ox2;
  359.         ty1 = std::round((((float)std::abs(oy1 - oy2) / (float)seg_size) * (float)i) + (float)oy1);
  360.         tx2 = lx2;
  361.         ty2 = std::round((((float)std::abs(ly1 - ly2) / (float)seg_size) * (float)i) + (float)ly1);
  362.         bg->Line(tx1, ty1, tx2, ty2, color);
  363.     }
  364.     // Marco
  365.     bg->Box(lx1, ly1, lx2, ly2, color);
  366. }
  367.  
  368.  
Add Comment
Please, Sign In to add comment