Advertisement
JoshDreamland

GSScreen.cpp

Jun 30th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.82 KB | None | 0 0
  1. /** Copyright (C) 2008-2012 Josh Ventura
  2. ***
  3. *** This file is a part of the ENIGMA Development Environment.
  4. ***
  5. *** ENIGMA is free software: you can redistribute it and/or modify it under the
  6. *** terms of the GNU General Public License as published by the Free Software
  7. *** Foundation, version 3 of the license or any later version.
  8. ***
  9. *** This application and its source code is distributed AS-IS, WITHOUT ANY
  10. *** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. *** FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  12. *** details.
  13. ***
  14. *** You should have received a copy of the GNU General Public License along
  15. *** with this code. If not, see <http://www.gnu.org/licenses/>
  16. **/
  17.  
  18. #include <string>
  19. #include <cstdio>
  20. #include "OpenGLHeaders.h"
  21. #include "GSbackground.h"
  22. #include "GSscreen.h"
  23. #include "GSd3d.h"
  24.  
  25. using namespace std;
  26.  
  27. #include "Universal_System/var4.h"
  28.  
  29. #define __GETR(x) (((unsigned int)x & 0x0000FF))
  30. #define __GETG(x) (((unsigned int)x & 0x00FF00) >> 8)
  31. #define __GETB(x) (((unsigned int)x & 0xFF0000) >> 16)
  32.  
  33. #include "Universal_System/roomsystem.h"
  34. #include "Universal_System/instance_system.h"
  35. #include "Universal_System/graphics_object.h"
  36. #include "Universal_System/depth_draw.h"
  37. #include "Platforms/platforms_mandatory.h"
  38. #include "Graphics_Systems/graphics_mandatory.h"
  39.  
  40. using namespace enigma;
  41.  
  42. static inline void draw_back()
  43. {
  44.     //Draw backgrounds
  45.     for (int back_current=0; back_current<7; back_current++)
  46.     {
  47.         if (background_visible[back_current] == 1)
  48.         {
  49.             // if (background_stretched) draw_background_stretched(back, x, y, w, h);
  50.             draw_background_tiled(background_index[back_current], background_x[back_current], background_y[back_current]);
  51.         }
  52.         // background_foreground, background_index, background_x, background_y, background_htiled,
  53.         // background_vtiled, background_hspeed, background_vspeed;
  54.     }
  55. }
  56.  
  57. namespace enigma
  58. {
  59.     extern std::map<int,roomstruct*> roomdata;
  60. }
  61.  
  62. void screen_redraw()
  63. {
  64.     int FBO;
  65.     if (!view_enabled)
  66.     {
  67.         glViewport(0, 0, window_get_width(), window_get_height());
  68.         glLoadIdentity();
  69.         if (GLEW_EXT_framebuffer_object)
  70.         {
  71.             glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &FBO);
  72.             glScalef(1, (FBO==0?-1:1), 1);
  73.         }
  74.         else
  75.         {
  76.             glScalef(1, -1, 1);
  77.         }
  78.         glOrtho(0, room_width, 0, room_height, 0, 1);
  79.         glGetDoublev(GL_MODELVIEW_MATRIX,projection_matrix);
  80.         glMultMatrixd(transformation_matrix);
  81.  
  82.         if (background_showcolor)
  83.         {
  84.             int clearcolor = ((int)background_color) & 0x00FFFFFF;
  85.             glClearColor(__GETR(clearcolor) / 255.0, __GETG(clearcolor) / 255.0, __GETB(clearcolor) / 255.0, 1);
  86.             glClear(GL_COLOR_BUFFER_BIT);
  87.         }
  88.         glClear(GL_DEPTH_BUFFER_BIT);
  89.  
  90.         draw_back();
  91.  
  92.         for (enigma::diter dit = drawing_depths.rbegin(); dit != drawing_depths.rend(); dit++)
  93.         {
  94.             //loop tiles
  95.             for(std::vector<tile>::size_type i = 0; i !=  dit->second.tiles.size(); i++)
  96.             {
  97.                 tile t = dit->second.tiles[i];
  98.                 draw_background_part(t.bckid, t.bgx, t.bgy, t.width, t.height, t.roomX, t.roomY);
  99.             }
  100.             enigma::inst_iter* push_it = enigma::instance_event_iterator;
  101.             for (enigma::instance_event_iterator = dit->second.draw_events->next; enigma::instance_event_iterator != NULL; enigma::instance_event_iterator = enigma::instance_event_iterator->next)
  102.                 enigma::instance_event_iterator->inst->myevent_draw();
  103.             enigma::instance_event_iterator = push_it;
  104.         }
  105.     }
  106.     else
  107.     {
  108.         int total_width = 0, total_height = 0;
  109.         for (int i = 0; i < 7; ++i) {
  110.             if (view_xview[i] + view_wview[i] > total_width) total_width = view_xview[i] + view_wview[i];
  111.             if (view_yview[i] + view_hview[i] > total_height) total_height = view_yview[i] + view_hview[i];
  112.     }
  113.     const double vxscale = window_get_width() / (double)total_width, vyscale = window_get_height() / (double)total_height;
  114.         for (view_current = 0; view_current < 7; view_current++)
  115.         {
  116.             if (view_visible[(int)view_current])
  117.             {
  118.                 int vc = (int)view_current;
  119.                 int vob = (int)view_object[vc];
  120.  
  121.                 if (vob != -1)
  122.                 {
  123.                     object_basic *instanceexists = fetch_instance_by_int(vob);
  124.  
  125.                     if (instanceexists)
  126.                     {
  127.                         object_planar* vobr = (object_planar*)instanceexists;
  128.  
  129.                         int vobx = (int)(vobr->x), voby = (int)(vobr->y);
  130.  
  131.                         //int bbl=*vobr.x+*vobr.bbox_left,bbr=*vobr.x+*vobr.bbox_right,bbt=*vobr.y+*vobr.bbox_top,bbb=*vobr.y+*vobr.bbox_bottom;
  132.                         //if (bbl<view_xview[vc]+view_hbor[vc]) view_xview[vc]=bbl-view_hbor[vc];
  133.  
  134.                         int vbc_h, vbc_v;
  135.                         (view_hborder[vc] > view_wview[vc]/2) ? vbc_h = int(view_wview[vc]/2) : vbc_h = view_hborder[vc];
  136.                         (view_vborder[vc] > view_hview[vc]/2) ? vbc_v = int(view_hview[vc]/2) : vbc_v = view_vborder[vc];
  137.  
  138.                         if (view_hspeed[vc] == -1)
  139.                         {
  140.                             if (vobx < view_xview[vc] + vbc_h)
  141.                                 view_xview[vc] = vobx - vbc_h;
  142.                             else if (vobx > view_xview[vc] + view_wview[vc] - vbc_h)
  143.                                 view_xview[vc] = vobx + vbc_h - view_wview[vc];
  144.                         }
  145.                         else
  146.                         {
  147.                             if (vobx < view_xview[vc] + vbc_h)
  148.                             {
  149.                                 view_xview[vc] -= view_hspeed[vc];
  150.                                 if (view_xview[vc] < vobx - vbc_h)
  151.                                     view_xview[vc] = vobx - vbc_h;
  152.                             }
  153.                             else if (vobx > view_xview[vc] + view_wview[vc] - vbc_h)
  154.                             {
  155.                                 view_xview[vc] += view_hspeed[vc];
  156.                                 if (view_xview[vc] > vobx + vbc_h - view_wview[vc])
  157.                                     view_xview[vc] = vobx + vbc_h - view_wview[vc];
  158.                             }
  159.                         }
  160.  
  161.                         if (view_vspeed[vc] == -1)
  162.                         {
  163.                             if (voby < view_yview[vc] + vbc_v)
  164.                                 view_yview[vc] = voby - vbc_v;
  165.                             else if (voby > view_yview[vc] + view_hview[vc] - vbc_v)
  166.                                 view_yview[vc] = voby + vbc_v - view_hview[vc];
  167.                         }
  168.                         else
  169.                         {
  170.                             if (voby < view_yview[vc] + vbc_v)
  171.                             {
  172.                                 view_yview[vc] -= view_vspeed[vc];
  173.                                 if (view_yview[vc] < voby - vbc_v)
  174.                                     view_yview[vc] = voby - vbc_v;
  175.                             }
  176.                             if (voby > view_yview[vc] + view_hview[vc] - vbc_v)
  177.                             {
  178.                                 view_yview[vc] += view_vspeed[vc];
  179.                                 if (view_yview[vc] > voby + vbc_v - view_hview[vc])
  180.                                     view_yview[vc] = voby + vbc_v - view_hview[vc];
  181.                             }
  182.                         }
  183.  
  184.                         if (view_xview[vc] < 0)
  185.                             view_xview[vc] = 0;
  186.                         else if (view_xview[vc] > room_width - view_wview[vc])
  187.                             view_xview[vc] = room_width - view_wview[vc];
  188.  
  189.                         if (view_yview[vc] < 0)
  190.                             view_yview[vc] = 0;
  191.                         else if (view_yview[vc] > room_height - view_hview[vc])
  192.                             view_yview[vc] = room_height - view_hview[vc];
  193.                     }
  194.                 }
  195.  
  196.                 glViewport((int)(view_xport[vc] * vxscale),(int)(view_yport[vc] * vyscale),(int)(view_wport[vc] * vxscale),(int)(view_hport[vc] * vyscale));
  197.                 glLoadIdentity();
  198.                 if (GLEW_EXT_framebuffer_object)
  199.                 {
  200.                     glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &FBO);
  201.                     glScalef(1, (FBO==0?-1:1), 1);
  202.                 }
  203.                 else
  204.                 {
  205.                     glScalef(1, -1, 1);
  206.                 }
  207.                 glOrtho(view_xview[vc], view_wview[vc] + view_xview[vc], view_yview[vc], view_hview[vc] + view_yview[vc], 0, 1);
  208.                 glGetDoublev(GL_MODELVIEW_MATRIX,projection_matrix);
  209.                 glMultMatrixd(transformation_matrix);
  210.  
  211.                 if (background_showcolor)
  212.                 {
  213.                     int clearcolor = ((int)background_color) & 0x00FFFFFF;
  214.                     glClearColor(__GETR(clearcolor) / 255.0, __GETG(clearcolor) / 255.0, __GETB(clearcolor) / 255.0, 1);
  215.                     glClear(GL_COLOR_BUFFER_BIT);
  216.                 }
  217.                 glClear(GL_DEPTH_BUFFER_BIT);
  218.  
  219.                 draw_back();
  220.  
  221.                 for (enigma::diter dit = drawing_depths.rbegin(); dit != drawing_depths.rend(); dit++)
  222.                 {
  223.                     //loop tiles
  224.                     for(std::vector<tile>::size_type i = 0; i !=  dit->second.tiles.size(); i++)
  225.                     {
  226.                         tile t = dit->second.tiles[i];
  227.                         if (t.roomX + t.width < view_xview[vc] || t.roomY + t.height < view_yview[vc] || t.roomX > view_xview[vc] + view_wview[vc] || t.roomY > view_yview[vc] + view_hview[vc])
  228.                             continue;
  229.  
  230.                         draw_background_part(t.bckid, t.bgx, t.bgy, t.width, t.height, t.roomX, t.roomY);
  231.                     }
  232.  
  233.                     enigma::inst_iter* push_it = enigma::instance_event_iterator;
  234.                     //loop instances
  235.                     for (enigma::instance_event_iterator = dit->second.draw_events->next; enigma::instance_event_iterator != NULL; enigma::instance_event_iterator = enigma::instance_event_iterator->next)
  236.                         enigma::instance_event_iterator->inst->myevent_draw();
  237.                     enigma::instance_event_iterator = push_it;
  238.                 }
  239.             }
  240.         }
  241.         view_current = 0;
  242.     }
  243. }
  244.  
  245. void screen_init()
  246. {
  247.     if (!view_enabled)
  248.     {
  249.         glMatrixMode(GL_PROJECTION);
  250.           glClearColor(0,0,0,0);
  251.           glLoadIdentity();
  252.           gluPerspective(0, 1, 0, 1);
  253.         glMatrixMode(GL_MODELVIEW);
  254.           glViewport(0, 0, window_get_width(), window_get_height());
  255.           glLoadIdentity();
  256.           glScalef(1, -1, 1);
  257.           glOrtho(0, room_width, 0, room_height, 0, 1);
  258.           glGetDoublev(GL_MODELVIEW_MATRIX,projection_matrix);
  259.           glMultMatrixd(transformation_matrix);
  260.           glClearColor(0,0,0,0);
  261.           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  262.           glDisable(GL_DEPTH_TEST);
  263.           glEnable(GL_BLEND);
  264.           glEnable(GL_ALPHA_TEST);
  265.           glEnable(GL_TEXTURE_2D);
  266.           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  267.           glAlphaFunc(GL_ALWAYS,0);
  268.           glColor4f(0,0,0,1);
  269.           glBindTexture(GL_TEXTURE_2D,0);
  270.     }
  271.     else
  272.     {
  273.         for (view_current = 0; view_current < 7; view_current++)
  274.         {
  275.             if (view_visible[(int)view_current])
  276.             {
  277.                 int vc = (int)view_current;
  278.                 glMatrixMode(GL_PROJECTION);
  279.                   glClearColor(0,0,0,0);
  280.                   glLoadIdentity();
  281.                   gluPerspective(0, 1, 0, 1);
  282.                 glMatrixMode(GL_MODELVIEW);
  283.                   glViewport(view_xport[vc], view_yport[vc], view_wport[vc], view_hport[vc]);
  284.                   glLoadIdentity();
  285.                   glScalef(1, -1, 1);
  286.                   glOrtho(view_xview[vc], view_wview[vc] + view_xview[vc], view_yview[vc], view_hview[vc] + view_yview[vc], 0, 1);
  287.                   glGetDoublev(GL_MODELVIEW_MATRIX,projection_matrix);
  288.                   glMultMatrixd(transformation_matrix);
  289.                   glClearColor(0,0,0,0);
  290.                   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  291.                   glDisable(GL_DEPTH_TEST);
  292.                   glEnable(GL_BLEND);
  293.                   glEnable(GL_ALPHA_TEST);
  294.                   glEnable(GL_TEXTURE_2D);
  295.                   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  296.                   glAlphaFunc(GL_ALWAYS,0);
  297.                   glColor4f(0,0,0,1);
  298.                   glBindTexture(GL_TEXTURE_2D,0);
  299.                 break;
  300.             }
  301.         }
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement