home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / BitmapControl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-13  |  6.2 KB  |  288 lines

  1. // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
  2. // Copyright (C) 1999-2003 Forgotten
  3. // Copyright (C) 2004 Forgotten and the VBA development team
  4.  
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2, or(at your option)
  8. // any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software Foundation,
  17. // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. // BitmapControl.cpp : implementation file
  20. //
  21.  
  22. #include "stdafx.h"
  23. #include "vba.h"
  24. #include "BitmapControl.h"
  25.  
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32. bool BitmapControl::isRegistered = false;
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // BitmapControl
  36.  
  37. IMPLEMENT_DYNCREATE(BitmapControl, CScrollView)
  38.  
  39.   BitmapControl::BitmapControl()
  40. {
  41.   w = 0;
  42.   h = 0;
  43.   data = NULL;
  44.   bmpInfo = NULL;
  45.   stretch = false;
  46.   registerClass();
  47.   CSize sizeTotal;
  48.   sizeTotal.cx = sizeTotal.cy = 0;
  49.   SetScrollSizes(MM_TEXT, sizeTotal);
  50. }
  51.  
  52. BitmapControl::~BitmapControl()
  53. {
  54. }
  55.  
  56.  
  57. BEGIN_MESSAGE_MAP(BitmapControl, CScrollView)
  58.   //{{AFX_MSG_MAP(BitmapControl)
  59.   ON_WM_ERASEBKGND()
  60.   ON_WM_SIZE()
  61.   ON_WM_LBUTTONDOWN()
  62.   //}}AFX_MSG_MAP
  63.   END_MESSAGE_MAP()
  64.  
  65.   /////////////////////////////////////////////////////////////////////////////
  66. // BitmapControl drawing
  67.  
  68. void BitmapControl::OnInitialUpdate()
  69. {
  70.   CScrollView::OnInitialUpdate();
  71.  
  72.   CSize sizeTotal;
  73.   // TODO: calculate the total size of this view
  74.   sizeTotal.cx = sizeTotal.cy = 100;
  75.   SetScrollSizes(MM_TEXT, sizeTotal);
  76. }
  77.  
  78. void BitmapControl::OnDraw(CDC* dc)
  79. {
  80.   RECT r;
  81.   GetClientRect(&r);
  82.   int w1 = r.right - r.left;
  83.   int h1 = r.bottom - r.top;
  84.   CDC memDC;
  85.   memDC.CreateCompatibleDC(dc);
  86.   if(!stretch) {
  87.     if(w > w1)
  88.       w1 = w;
  89.     if(h > h1)
  90.       h1 = h;
  91.   }
  92.   CBitmap bitmap, *pOldBitmap;
  93.   bitmap.CreateCompatibleBitmap(dc, w1, h1);
  94.   pOldBitmap = memDC.SelectObject(&bitmap);
  95.   if(stretch) {
  96.     bmpInfo->bmiHeader.biWidth = w;
  97.     bmpInfo->bmiHeader.biHeight = -h;
  98.     
  99.     StretchDIBits(memDC.GetSafeHdc(),
  100.                   0,
  101.                   0,
  102.                   w1,
  103.                   h1, 
  104.                   0,
  105.                   0,
  106.                   w,
  107.                   h,
  108.                   data,
  109.                   bmpInfo,
  110.                   DIB_RGB_COLORS,
  111.                   SRCCOPY);
  112.   } else {
  113.     FillOutsideRect(&memDC, CBrush::FromHandle(GetSysColorBrush(COLOR_BTNFACE)));
  114.     
  115.     bmpInfo->bmiHeader.biWidth = w;
  116.     bmpInfo->bmiHeader.biHeight = -h;
  117.     SetDIBitsToDevice(memDC.GetSafeHdc(),
  118.                       0,
  119.                       0,
  120.                       w,
  121.                       h,
  122.                       0,
  123.                       0,
  124.                       0,
  125.                       h,
  126.                       data,
  127.                       bmpInfo,
  128.                       DIB_RGB_COLORS);
  129.   }
  130.  
  131.   dc->BitBlt(0,0,w1,h1,
  132.              &memDC,0,0,SRCCOPY);
  133.   memDC.SelectObject(pOldBitmap);
  134.  
  135.   bitmap.DeleteObject();
  136.   memDC.DeleteDC();  
  137. }
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // BitmapControl diagnostics
  141.  
  142. #ifdef _DEBUG
  143. void BitmapControl::AssertValid() const
  144. {
  145.   CScrollView::AssertValid();
  146. }
  147.  
  148. void BitmapControl::Dump(CDumpContext& dc) const
  149. {
  150.   CScrollView::Dump(dc);
  151. }
  152. #endif //_DEBUG
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. // BitmapControl message handlers
  156.  
  157. BOOL BitmapControl::OnEraseBkgnd(CDC* pDC) 
  158. {
  159.   return TRUE;
  160. }
  161.  
  162. void BitmapControl::OnSize(UINT nType, int cx, int cy) 
  163. {
  164.   if(!stretch)
  165.     CScrollView::OnSize(nType, cx, cy);
  166. }
  167.  
  168. void BitmapControl::OnLButtonDown(UINT nFlags, CPoint pt) 
  169. {
  170.   if(!data)
  171.     return;
  172.   int x = pt.x;
  173.   int y = pt.y;
  174.  
  175.   WPARAM point;
  176.   
  177.   if(stretch) {
  178.     RECT rect;
  179.     GetClientRect(&rect);
  180.   
  181.     int height = rect.bottom - rect.top;
  182.     int width = rect.right - rect.left;
  183.   
  184.     int xx = (x * w) / width;
  185.     int yy = (y * h) / height;
  186.  
  187.     point = xx | (yy<<16);
  188.  
  189.     int xxx = xx / 8;
  190.     int yyy = yy / 8;
  191.  
  192.     for(int i = 0; i < 8; i++) {
  193.       memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
  194.                                    w * yyy * 8 * 3 +
  195.                                    i * w * 3], 8 * 3);
  196.     }
  197.   } else {
  198.     POINT p;
  199.     p.x = GetScrollPos(SB_HORZ);
  200.     p.y = GetScrollPos(SB_VERT);
  201.  
  202.     p.x += x;
  203.     p.y += y;
  204.  
  205.     if(p.x >= w ||
  206.        p.y >= h)
  207.       return;
  208.  
  209.     point = p.x | (p.y<<16);
  210.     
  211.     int xxx = p.x / 8;
  212.     int yyy = p.y / 8;
  213.  
  214.     for(int i = 0; i < 8; i++) {
  215.       memcpy(&colors[i*3*8], &data[xxx * 8 * 3 +
  216.                                    w * yyy * 8 * 3 +
  217.                                    i * w * 3], 8 * 3);
  218.     }
  219.   }
  220.   
  221.   GetParent()->SendMessage(WM_MAPINFO,
  222.                            point,
  223.                            (LPARAM)colors);
  224. }
  225.  
  226. void BitmapControl::setBmpInfo(BITMAPINFO *info)
  227. {
  228.   bmpInfo = info;
  229. }
  230.  
  231. void BitmapControl::setData(u8 *d)
  232. {
  233.   data = d;
  234. }
  235.  
  236. void BitmapControl::setSize(int w1, int h1)
  237. {
  238.   if(w != w1 || h != h1) {
  239.     w = w1;
  240.     h = h1;
  241.     SIZE s;
  242.     s.cx = w;
  243.     s.cy = h;
  244.     SetScrollSizes(MM_TEXT, s);
  245.   }
  246. }
  247.  
  248. void BitmapControl::refresh()
  249. {
  250.   Invalidate();
  251. }
  252.  
  253.  
  254. void BitmapControl::registerClass()
  255. {
  256.   if(!isRegistered) {
  257.     WNDCLASS wc;
  258.     ZeroMemory(&wc, sizeof(wc));
  259.     wc.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
  260.     wc.lpfnWndProc = (WNDPROC)::DefWindowProc;
  261.     wc.hInstance = AfxGetInstanceHandle();
  262.     wc.hIcon = NULL;
  263.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  264.     wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  265.     wc.lpszMenuName = NULL;
  266.     wc.lpszClassName = "VbaBitmapControl";
  267.     AfxRegisterClass(&wc);
  268.     isRegistered = true;
  269.   }
  270. }
  271.  
  272. void BitmapControl::setStretch(bool b)
  273. {
  274.   if(b != stretch) {
  275.     stretch = b;
  276.     Invalidate();
  277.   }
  278. }
  279.  
  280. bool BitmapControl::getStretch()
  281. {
  282.   return stretch;
  283. }
  284.  
  285. void BitmapControl::PostNcDestroy() 
  286. {
  287. }
  288.