Advertisement
Kitomas

2024-07-19 (9/13)

Jul 19th, 2024
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.42 KB | None | 0 0
  1. /******************************************************************************/
  2. /******************************************************************************/
  3. //"kit_w32\include\kit\_video_Window.hpp":
  4. #ifndef _KIT_INC__VIDEO_WINDOW_HPP
  5. #define _KIT_INC__VIDEO_WINDOW_HPP
  6.  
  7. #include "commondef.hpp"
  8. #include "_video_common.hpp"
  9. #include "_video_WindowEvent.hpp"
  10.  
  11.  
  12. namespace kit {
  13.  
  14.  
  15.  
  16.  
  17. enum WindowPositionEnum {
  18.   WINPOS_UNDEFINED = 0xC0000000,
  19.   WINPOS_CENTERED  = 0xC0000001,
  20.   WINPOS_UNCHANGED = 0xC0000002,
  21. };
  22.  
  23.  
  24. //(some flags override others!)
  25. enum WindowFlagEnum {
  26.   WINFLAG_HIDDEN       = 0x001, // window starts invisible
  27.   WINFLAG_BORDERLESS   = 0x002, // window lacks any decoration
  28.   WINFLAG_RESIZABLE    = 0x004, // window can be resized
  29.   WINFLAG_MINIMIZED    = 0x008, // window starts minimized
  30.   WINFLAG_MAXIMIZED    = 0x010, // window starts maximized
  31.   WINFLAG_FOCUS        = 0x020, // window has grabbed input focus
  32.   WINFLAG_FULLSCREEN   = 0x040, // window starts in fullscreen mode (non-exclusive)
  33.   WINFLAG_REM_MINIMIZE = 0x080, // window lacks a minimize button
  34.   WINFLAG_REM_MAXIMIZE = 0x100, // window lacks a maximize button
  35. };
  36.  
  37.  
  38.  
  39.  
  40. class Bitmap;
  41. struct _WindowOpaque;
  42.  
  43. class Window {
  44.   u32 _type;
  45.   bool _valid         = false;
  46.   bool _constructing  = true;
  47.   u16 _padding16;
  48.   _WindowOpaque* _opq = nullptr;
  49.   char _title[256];
  50.  
  51.  
  52. public:
  53.  
  54.   Window(const char* windowTitle,
  55.          s32 windowWidth, s32 windowHeight,
  56.          u32 windowFlags = 0,
  57.          s32 windowX = WINPOS_UNDEFINED,
  58.          s32 windowY = WINPOS_UNDEFINED,
  59.          s32 canvasWidth = 0, s32 canvasHeight = 0,
  60.          bool directAccess = false);
  61.  
  62.   ~Window();
  63.  
  64.  
  65.   bool isValid(){ return _valid; }
  66.   bool isConstructing(){ return _constructing; }
  67.   bool isClosed();
  68.  
  69.   const char* getTitle(){ return _title; }
  70.   shape::rect  getWindowRect();
  71.   shape::point getCanvasSize();
  72.   color::ARGB* getPixels();
  73.  
  74.   void setWindowRect(const shape::rect* newRect);
  75.   void setCanvasSize(s32 width, s32 height);
  76.   void setMinimized(bool minimize);
  77.   void setMaximized(bool maximize);
  78.   void setFullscreen(bool fullscreen);
  79.   void setVisibility(bool show);
  80.   void setFocus(bool enable = true);
  81.   void setCanvasInterpolationMode(bool halftone); //false for nearest-neighbor
  82.   void setBitmapInterpolationMode(bool halftone);  //
  83.  
  84.   void lock(bool locked = true);
  85.   void unlock(){ lock(false); }
  86.  
  87.   //if updateImmediately is false, present only 'schedules' a future redraw
  88.   void present(bool updateImmediately = true);
  89.   void clear(color::ARGB color = 0x00000000);
  90.  
  91.   void drawPoint(shape::point point, color::ARGB color);
  92.   void drawPoints(shape::point* points, size_t points_len,
  93.                   color::ARGB color, bool useSetPixel = false);
  94.  
  95.   void drawLines(shape::point* lineEnds, size_t lineEnds_len,
  96.                  color::ARGB color, u32 width = 1);
  97.   void drawLineSegments(shape::line* lines, size_t lines_len,
  98.                         color::ARGB color, u32 width = 1);
  99.  
  100.   void drawRectangles(shape::rect* rects, size_t rects_len, color::ARGB color);
  101. };
  102.  
  103.  
  104.  
  105.  
  106. };
  107.  
  108. #endif /* _KIT__VIDEO_WINDOW_HPP */
  109. /******************************************************************************/
  110. /******************************************************************************/
  111. //"kit_w32\include\kit\_video_WindowEvent.hpp":
  112. #ifndef _KIT_INC__VIDEO_WINDOWEVENT_HPP
  113. #define _KIT_INC__VIDEO_WINDOWEVENT_HPP
  114.  
  115. #include "commondef.hpp"
  116.  
  117.  
  118. namespace kit {
  119.  
  120.  
  121.  
  122.  
  123. #define KIT_EVENT_ID(_id) ( (_id) & 0xFFFF0000 )
  124. #define KIT_SUBEVENT_ID(_id) ( (_id) & 0xFFFF )
  125.  
  126. enum WindowEventEnum {
  127.   WINEVENT_NULL           = 0x00000000,
  128.  
  129.   WINEVENT_COMMON         = 0x00010000, //WindowEvent_Common
  130.  
  131.   WINEVENT_WIN            = 0x00020000, //WindowEvent_Win
  132.   WINEVENT_WIN_CLOSE      = WINEVENT_WIN   | 0x0001,
  133.   WINEVENT_WIN_MOVED      = WINEVENT_WIN   | 0x0002,
  134.   WINEVENT_WIN_RESIZED    = WINEVENT_WIN   | 0x0003,
  135.   WINEVENT_WIN_UNFOCUSING = WINEVENT_WIN   | 0x0004,
  136.   WINEVENT_WIN_FOCUSED    = WINEVENT_WIN   | 0x0005,
  137.   WINEVENT_WIN_MINIMIZED  = WINEVENT_WIN   | 0x0006, //(not implemented at all)
  138.   WINEVENT_WIN_MAXIMIZED  = WINEVENT_WIN   | 0x0007, //(not implemented at all)
  139.   WINEVENT_WIN_RESTORED   = WINEVENT_WIN   | 0x0008, //(not implemented at all)
  140.  
  141.   WINEVENT_KEY            = 0x00030000, //WindowEvent_Key
  142.   WINEVENT_KEY_CHAR       = WINEVENT_KEY   | 0x0001,
  143.   WINEVENT_KEY_UP         = WINEVENT_KEY   | 0x0002,
  144.   WINEVENT_KEY_DOWN       = WINEVENT_KEY   | 0x0003,
  145.  
  146.   WINEVENT_MOUSE          = 0x00040000, //WindowEvent_Mouse
  147.   WINEVENT_MOUSE_MOVE     = WINEVENT_MOUSE | 0x0001,
  148.   WINEVENT_MOUSE_LEAVE    = WINEVENT_MOUSE | 0x0002, //(not fully implemented)
  149.   WINEVENT_MOUSE_ENTER    = WINEVENT_MOUSE | 0x0003, //(not fully implemented)
  150.   WINEVENT_MOUSE_UP       = WINEVENT_MOUSE | 0x0004,
  151.   WINEVENT_MOUSE_DOWN     = WINEVENT_MOUSE | 0x0005,
  152.   WINEVENT_MOUSE_HWHEEL   = WINEVENT_MOUSE | 0x0006,
  153.   WINEVENT_MOUSE_VWHEEL   = WINEVENT_MOUSE | 0x0007,
  154. };
  155.  
  156.  
  157.  
  158. /*+WINEVENT_COMMON+*/
  159.  
  160. struct WindowEvent_Common { //16B
  161.   u32 type;
  162.   u32 winIndex;
  163.   u64 timestamp;
  164. };
  165.  
  166. /*-WINEVENT_COMMON-*/
  167.  
  168.  
  169.  
  170. /*+WINEVENT_WIN+*/
  171.  
  172. struct WindowEvent_Win { //24B
  173.   u32 type;
  174.   u32 winIndex;
  175.   u64 timestamp;
  176.   union { s32 data1, dataX; };
  177.   union { s32 data2, dataY; };
  178. };
  179.  
  180. /*-WINEVENT_WIN-*/
  181.  
  182.  
  183.  
  184. /*+WINEVENT_KEY+*/
  185.  
  186. enum WindowEvent_Key_ModifierEnum {
  187.   KMOD_NONE      = 0x0000,
  188.   KMOD_LSHIFT    = 0x0001,
  189.   KMOD_RSHIFT    = 0x0002,
  190.   KMOD_LCTRL     = 0x0004,
  191.   KMOD_RCTRL     = 0x0008,
  192.   KMOD_LALT      = 0x0010,
  193.   KMOD_RALT      = 0x0020,
  194.   KMOD_LGUI      = 0x0040, //windows key?
  195.   KMOD_RGUI      = 0x0080,  //
  196.     KMOD_LWIN    = KMOD_LGUI,
  197.     KMOD_RWIN    = KMOD_RGUI,
  198.   KMOD_NUMLOCK   = 0x1000,
  199.   KMOD_CAPSLOCK  = 0x2000,
  200.   KMOD_ALTGRAPH  = 0x4000,
  201.   KMOD_SCROLLOCK = 0x8000,
  202.   KMOD_CTRL      = ( KMOD_LCTRL  | KMOD_RCTRL  ),
  203.   KMOD_SHIFT     = ( KMOD_LSHIFT | KMOD_RSHIFT ),
  204.   KMOD_ALT       = ( KMOD_LALT   | KMOD_RALT   ),
  205.   KMOD_GUI       = ( KMOD_LGUI   | KMOD_RGUI   ),
  206.   KMOD_WIN       = ( KMOD_LWIN   | KMOD_RWIN   ),
  207. };
  208.  
  209. enum WindowEvent_Key_PhysicalEnum {
  210.   //tbd because i don't want to bother with scancode stuff right now
  211. };
  212.  
  213. enum WindowEvent_Key_VirtualEnum {
  214.   //(misc. mouse)       = 0x01 -> 0x06
  215.   //(reserved)          = 0x07,
  216.   VKEY_BACK             = 0x08, //backspace key
  217.     VKEY_BACKSPACE      = VKEY_BACK,
  218.   VKEY_TAB              = 0x09,
  219.   //(reserved)          = 0x0A -> 0x0B,
  220.   VKEY_CLEAR            = 0x0C,
  221.   VKEY_RETURN           = 0x0D, //enter key
  222.     VKEY_ENTER          = VKEY_RETURN,
  223.   //(unassigned)        = 0x0E -> 0x0F,
  224.   VKEY_SHIFT            = 0x10,
  225.   VKEY_CONTROL          = 0x11, //ctrl key
  226.     VKEY_CTRL           = VKEY_CONTROL,
  227.   VKEY_MENU             = 0x12, //alt key
  228.     VKEY_ALT            = VKEY_MENU,
  229.   VKEY_PAUSE            = 0x13,
  230.   VKEY_CAPITAL          = 0x14, //caps lock key
  231.     VKEY_CAPSLOCK       = VKEY_CAPITAL,
  232.   //(IME stuff)         = 0x15 -> 0x1A,
  233.   VKEY_ESCAPE           = 0x1B, //esc key
  234.   //(IME stuff)         = 0x1C -> 0x1F,
  235.   VKEY_SPACE            = 0x20, //space bar key
  236.   VKEY_PRIOR            = 0x21, //page up key
  237.     VKEY_PGUP           = VKEY_PRIOR,
  238.   VKEY_NEXT             = 0x22, //page down key
  239.     VKEY_PGDN           = VKEY_NEXT,
  240.   VKEY_END              = 0x23,
  241.   VKEY_HOME             = 0x24,
  242.   VKEY_LEFT             = 0x25, //left arrow key
  243.   VKEY_UP               = 0x26, //up arrow key
  244.   VKEY_RIGHT            = 0x27, //right arrow key
  245.   VKEY_DOWN             = 0x28, //down arrow key
  246.   VKEY_SELECT           = 0x29,
  247.   VKEY_PRINT            = 0x2A,
  248.   VKEY_EXECUTE          = 0x2B,
  249.   VKEY_SNAPSHOT         = 0x2C, //print screen key
  250.     VKEY_PRTSC          = VKEY_SNAPSHOT,
  251.   VKEY_INSERT           = 0x2D, //ins key
  252.   VKEY_DELETE           = 0x2E, //del key
  253.   VKEY_HELP             = 0x2F, //help key
  254.   VKEY_0                = 0x30, //'0'
  255.   VKEY_1                = 0x31, //'1'
  256.   VKEY_2                = 0x32, //'2'
  257.   VKEY_3                = 0x33, //'3'
  258.   VKEY_4                = 0x34, //'4'
  259.   VKEY_5                = 0x35, //'5'
  260.   VKEY_6                = 0x36, //'6'
  261.   VKEY_7                = 0x37, //'7'
  262.   VKEY_8                = 0x38, //'8'
  263.   VKEY_9                = 0x39, //'9'
  264.   //(undefined)         = 0x3A -> 0x40,
  265.   VKEY_A                = 0x41, //'A'
  266.   VKEY_B                = 0x42, //'B'
  267.   VKEY_C                = 0x43, //'C'
  268.   VKEY_D                = 0x44, //'D'
  269.   VKEY_E                = 0x45, //'E'
  270.   VKEY_F                = 0x46, //'F'
  271.   VKEY_G                = 0x47, //'G'
  272.   VKEY_H                = 0x48, //'H'
  273.   VKEY_I                = 0x49, //'I'
  274.   VKEY_J                = 0x4A, //'J'
  275.   VKEY_K                = 0x4B, //'K'
  276.   VKEY_L                = 0x4C, //'L'
  277.   VKEY_M                = 0x4D, //'M'
  278.   VKEY_N                = 0x4E, //'N'
  279.   VKEY_O                = 0x4F, //'O'
  280.   VKEY_P                = 0x50, //'P'
  281.   VKEY_Q                = 0x51, //'Q'
  282.   VKEY_R                = 0x52, //'R'
  283.   VKEY_S                = 0x53, //'S'
  284.   VKEY_T                = 0x54, //'T'
  285.   VKEY_U                = 0x55, //'U'
  286.   VKEY_V                = 0x56, //'V'
  287.   VKEY_W                = 0x57, //'W'
  288.   VKEY_X                = 0x58, //'X'
  289.   VKEY_Y                = 0x59, //'Y'
  290.   VKEY_Z                = 0x5A, //'Z'
  291.   VKEY_LWIN             = 0x5B, //left windows key
  292.   VKEY_RWIN             = 0x5C, //right windows key
  293.   VKEY_APPS             = 0x5D, //applications key
  294.   //(reserved)          = 0x5E,
  295.   VKEY_SLEEP            = 0x5F, //computer sleep key
  296.   VKEY_NUMPAD0          = 0x60,
  297.   VKEY_NUMPAD1          = 0x61,
  298.   VKEY_NUMPAD2          = 0x62,
  299.   VKEY_NUMPAD3          = 0x63,
  300.   VKEY_NUMPAD4          = 0x64,
  301.   VKEY_NUMPAD5          = 0x65,
  302.   VKEY_NUMPAD6          = 0x66,
  303.   VKEY_NUMPAD7          = 0x67,
  304.   VKEY_NUMPAD8          = 0x68,
  305.   VKEY_NUMPAD9          = 0x69,
  306.   VKEY_MULTIPLY         = 0x6A, //numpad '*'
  307.   VKEY_ADD              = 0x6B, //numpad '+'
  308.   VKEY_SEPARATOR        = 0x6C, //numpad enter
  309.   VKEY_SUBTRACT         = 0x6D, //numpad '-'
  310.   VKEY_DECIMAL          = 0x6E, //numpad '.'
  311.   VKEY_DIVIDE           = 0x6F, //numpad '/'
  312.   VKEY_F1               = 0x70,
  313.   VKEY_F2               = 0x71,
  314.   VKEY_F3               = 0x72,
  315.   VKEY_F4               = 0x73,
  316.   VKEY_F5               = 0x74,
  317.   VKEY_F6               = 0x75,
  318.   VKEY_F7               = 0x76,
  319.   VKEY_F8               = 0x77,
  320.   VKEY_F9               = 0x78,
  321.   VKEY_F10              = 0x79,
  322.   VKEY_F11              = 0x7A,
  323.   VKEY_F12              = 0x7B,
  324.   VKEY_F13              = 0x7C,
  325.   VKEY_F14              = 0x7D,
  326.   VKEY_F15              = 0x7E,
  327.   VKEY_F16              = 0x7F,
  328.   VKEY_F17              = 0x80,
  329.   VKEY_F18              = 0x81,
  330.   VKEY_F19              = 0x82,
  331.   VKEY_F20              = 0x83,
  332.   VKEY_F21              = 0x84,
  333.   VKEY_F22              = 0x85,
  334.   VKEY_F23              = 0x86,
  335.   VKEY_F24              = 0x87,
  336.   //(reserved)          = 0x88 -> 0x8F,
  337.   VKEY_NUMLOCK          = 0x90,
  338.   VKEY_SCROLL           = 0x91, //scroll lock key
  339.     VKEY_SCROLLOCK      = VKEY_SCROLL,
  340.   //(OEM-specific)      = 0x92 -> 0x96,
  341.   //(unassigned)        = 0x97 -> 0x9F,
  342.   //(l/r key variants)  = 0xA0 -> 0xA5,
  343.   //(browser keys)      = 0xA6 -> 0xAC,
  344.   VKEY_VOLUME_MUTE      = 0xAD,
  345.   VKEY_VOLUME_DOWN      = 0xAE,
  346.   VKEY_VOLUME_UP        = 0xAF,
  347.   VKEY_MEDIA_NEXT_TRACK = 0xB0,
  348.   VKEY_MEDIA_PREV_TRACK = 0xB1,
  349.   VKEY_MEDIA_STOP       = 0xB2,
  350.   VKEY_MEDIA_PLAY_PAUSE = 0xB3, //Play/Pause Media key
  351.   //(launch keys)       = 0xB4 -> 0xB7,
  352.   //(reserved)          = 0xB8 -> 0xB9,
  353.   VKEY_OEM_1            = 0xBA, //misc. chars; varies by keyboard (';',':' on US standard)
  354.     VKEY_SEMICOLON      = VKEY_OEM_1,
  355.   VKEY_OEM_PLUS         = 0xBB, //'+' in any country/region
  356.     VKEY_PLUS           = VKEY_OEM_PLUS,
  357.   VKEY_OEM_COMMA        = 0xBC, //',' in any country/region
  358.     VKEY_COMMA          = VKEY_OEM_COMMA,
  359.   VKEY_OEM_MINUS        = 0xBD, //'-' in any country/region
  360.     VKEY_MINUS          = VKEY_OEM_MINUS,
  361.   VKEY_OEM_PERIOD       = 0xBE, //'.' in any country/region
  362.     VKEY_PERIOD         = VKEY_OEM_PERIOD,
  363.   VKEY_OEM_2            = 0xBF, //misc. chars; varies by keyboard ('/','?' on US standard)
  364.     VKEY_FSLASH         = VKEY_OEM_2,
  365.   VKEY_OEM_3            = 0xC0, //misc. chars; varies by keyboard ('`','~' on US standard)
  366.     VKEY_BACKTICK       = VKEY_OEM_3,
  367.   //(reserved)          = 0xC1 -> 0xDA,
  368.   VKEY_OEM_4            = 0xDB, //misc. chars; varies by keyboard ('[','{' on US standard)
  369.     VKEY_LBRACKET       = VKEY_OEM_4,
  370.   VKEY_OEM_5            = 0xDC, //misc. chars; varies by keyboard ('\\','|' on US standard)
  371.     VKEY_BSLASH         = VKEY_OEM_5,
  372.   VKEY_OEM_6            = 0xDD, //misc. chars; varies by keyboard (']','}' on US standard)
  373.     VKEY_RBRACKET       = VKEY_OEM_6,
  374.   VKEY_OEM_7            = 0xDE, //misc. chars; varies by keyboard ('\'','\"' on US standard)
  375.     VKEY_APOSTROPHE     = VKEY_OEM_7,
  376.   VKEY_OEM_8            = 0xDF, //misc. chars; varies by keyboard
  377.   //(reserved)          = 0xE0,
  378.   //(misc.)             = 0xE1 -> 0xE7,
  379.   //(unassigned)        = 0xE8,
  380.   //(misc.)             = 0xE9 -> 0xFE,
  381. };
  382.  
  383.  
  384. union WindowEvent_Key_Mod { //2B
  385.   struct {
  386.     u16 lshift    : 1;
  387.     u16 rshift    : 1;
  388.     u16 lctrl     : 1;
  389.     u16 rctrl     : 1;
  390.     u16 lalt      : 1;
  391.     u16 ralt      : 1;
  392.     u16 lgui      : 1;
  393.     u16 rgui      : 1;
  394.     u16 _unused   : 4;
  395.     u16 numlock   : 1;
  396.     u16 capslock  : 1;
  397.     u16 altgraph  : 1;
  398.     u16 scrollock : 1;
  399.   };
  400.   u16 all;
  401. };
  402.  
  403. struct WindowEvent_Key_Sym { //8B
  404.   union {
  405.     WindowEvent_Key_Mod kmod;
  406.     u16  kmods;
  407.   };
  408.   u8  _unused;
  409.   u8   pkey; //physical key code (named .scancode in SDL's Keysym struct)
  410.   u8   vkey; //virtual key code (named .sym in SDL's Keysym struct)
  411.   bool pressed;
  412.   bool ischar; //'is event KEY_CHAR?', otherwise it's KEY_UP or KEY_DOWN
  413.   bool repeat;
  414. };
  415.  
  416. struct WindowEvent_Key { //24B
  417.   u32 type;
  418.   u32 winIndex;
  419.   u64 timestamp;
  420.   union {
  421.     struct { //(basically a copy of WindowEvent_Key_Sym)
  422.       u16  kmods;
  423.       u8  _unused;
  424.       u8   pkey;
  425.       u8   vkey;
  426.       bool pressed;
  427.       bool ischar;
  428.       bool repeat;
  429.     };
  430.     WindowEvent_Key_Sym sym;
  431.   };
  432. };
  433.  
  434. /*-WINEVENT_KEY-*/
  435.  
  436.  
  437.  
  438. /*+WINEVENT_MOUSE+*/
  439.  
  440. //how many units between mouse wheel notches
  441. #define KIT_MOUSE_WHEEL_DELTA (120)
  442.  
  443.  
  444. enum WindowEvent_Mouse_ButtonEnum {
  445.   MBUTTON_LEFT   = 0x01,
  446.   MBUTTON_MIDDLE = 0x02,
  447.   MBUTTON_RIGHT  = 0x04,
  448.   MBUTTON_X1     = 0x08,
  449.   MBUTTON_X2     = 0x10,
  450. };
  451.  
  452.  
  453. struct WindowEvent_Mouse { //40B
  454.   u32  type;
  455.   u32  winIndex;
  456.   u64  timestamp;
  457.   s32  x;  //coordinates relative to window
  458.   s32  y;   //^^
  459.   s32  dx; //delta x (coordinates relative to last recorded x position)
  460.   s32  dy; //delta y (coordinates relative to last recorded y position)
  461.   s32  data; //currently used to store the value of wheel events before delta is applied
  462.   u8  _unused;
  463.   u8   button;   //flags for currently pressed buttons (WindowEvent_Mouse_ButtonEnum)
  464.   bool pressed;  //will be true if button is nonzero
  465.   bool dblClick; //'is double click?'
  466. };
  467.  
  468. /*-WINEVENT_MOUSE-*/
  469.  
  470.  
  471.  
  472. union WindowEvent { //<whatever the largest event is>B
  473.   u32                type;
  474.   WindowEvent_Common common;
  475.   WindowEvent_Win    win;
  476.   WindowEvent_Key    key;
  477.   WindowEvent_Mouse  mouse;
  478.   WindowEvent() : type(WINEVENT_NULL) {}
  479. };
  480.  
  481.  
  482.  
  483.  
  484. };
  485.  
  486. #endif /* _KIT_INC__VIDEO_WINDOWEVENT_HPP */
  487. /******************************************************************************/
  488. /******************************************************************************/
  489. //"kit_w32\src\test_main_utils.hpp":
  490. #ifndef TEST_MAIN_UTILS_HPP
  491. #define TEST_MAIN_UTILS_HPP
  492.  
  493.  
  494. #if defined(_DEBUG)
  495. #include <stdio.h>
  496. #define loghere printf("line %4i: (%s)\n",__LINE__,__FILE__);
  497. #else
  498. #define loghere ; /* do {} while(0); */
  499. #endif /* _DEBUG */
  500.  
  501.  
  502. const char* getBool(bool state){ return (state) ? "true" : "false"; }
  503.  
  504. void printEvent(kit::WindowEvent& event){
  505.   switch(event.type){
  506.     case kit::WINEVENT_NULL          :
  507.     /*
  508.     case kit::WINEVENT_COMMON        :
  509.     case kit::WINEVENT_WIN_CLOSE     :
  510.     case kit::WINEVENT_WIN_MOVED     :
  511.     case kit::WINEVENT_WIN_RESIZED   :
  512.     case kit::WINEVENT_WIN_UNFOCUSING:
  513.     case kit::WINEVENT_WIN_FOCUSED   :
  514.     case kit::WINEVENT_KEY_CHAR      :
  515.     case kit::WINEVENT_KEY_UP        :
  516.     case kit::WINEVENT_KEY_DOWN      :
  517.     case kit::WINEVENT_MOUSE_MOVE    :
  518.     case kit::WINEVENT_MOUSE_LEAVE   :
  519.     case kit::WINEVENT_MOUSE_ENTER   :
  520.     case kit::WINEVENT_MOUSE_UP      :
  521.     case kit::WINEVENT_MOUSE_DOWN    :
  522.     case kit::WINEVENT_MOUSE_HWHEEL  :
  523.     case kit::WINEVENT_MOUSE_VWHEEL  : */
  524.                                        return;
  525.     default                          : break;
  526.   }
  527.  
  528.   printf("WINEVENT_");
  529.   switch(event.type){
  530.     case kit::WINEVENT_NULL          : printf("NULL");           break;
  531.     case kit::WINEVENT_COMMON        : printf("COMMON");         break;
  532.     case kit::WINEVENT_WIN_CLOSE     : printf("WIN_CLOSE");      break;
  533.     case kit::WINEVENT_WIN_MOVED     : printf("WIN_MOVED");      break;
  534.     case kit::WINEVENT_WIN_RESIZED   : printf("WIN_RESIZED");    break;
  535.     case kit::WINEVENT_WIN_UNFOCUSING: printf("WIN_UNFOCUSING"); break;
  536.     case kit::WINEVENT_WIN_FOCUSED   : printf("WIN_FOCUSED");    break;
  537.     case kit::WINEVENT_KEY_CHAR      : printf("KEY_CHAR");       break;
  538.     case kit::WINEVENT_KEY_UP        : printf("KEY_UP");         break;
  539.     case kit::WINEVENT_KEY_DOWN      : printf("KEY_DOWN");       break;
  540.     case kit::WINEVENT_MOUSE_MOVE    : printf("MOUSE_MOVE");     break;
  541.     case kit::WINEVENT_MOUSE_LEAVE   : printf("MOUSE_LEAVE");    break;
  542.     case kit::WINEVENT_MOUSE_ENTER   : printf("MOUSE_ENTER");    break;
  543.     case kit::WINEVENT_MOUSE_UP      : printf("MOUSE_UP");       break;
  544.     case kit::WINEVENT_MOUSE_DOWN    : printf("MOUSE_DOWN");     break;
  545.     case kit::WINEVENT_MOUSE_HWHEEL  : printf("MOUSE_HWHEEL");   break;
  546.     case kit::WINEVENT_MOUSE_VWHEEL  : printf("MOUSE_VWHEEL");   break;
  547.     default                          : printf("(unknown)");      break;
  548.   }
  549.  
  550.   printf(" (0x%08X): {\n",event.type);
  551.  
  552.   if(event.type != kit::WINEVENT_NULL){
  553.     printf("  winIndex  = %u,\n",   event.common.winIndex);
  554.     printf("  timestamp = %llu,\n", event.common.timestamp);
  555.   }
  556.  
  557.  
  558.   switch(KIT_EVENT_ID(event.type)){
  559.     case kit::WINEVENT_WIN  :
  560.     {
  561.       printf("  dataX = %i,\n", event.win.dataX);
  562.       printf("  dataY = %i,\n", event.win.dataY);
  563.     } break;
  564.  
  565.     case kit::WINEVENT_KEY  :
  566.     {
  567.       printf("  kmods   = 0x%04X,\n",              event.key.kmods);
  568.       printf("  pkey    = \'%c\' (0x%02X),\n",     event.key.pkey, event.key.pkey);
  569.       printf("  vkey    = \'%c\' (0x%02X),\n",     event.key.vkey, event.key.vkey);
  570.       printf("  pressed = %s,\n",          getBool(event.key.pressed));
  571.       printf("  ischar  = %s,\n",          getBool(event.key.ischar));
  572.       printf("  repeat  = %s,\n",          getBool(event.key.repeat));
  573.     } break;
  574.  
  575.     case kit::WINEVENT_MOUSE:
  576.     {
  577.       printf("  x        = %i,\n",        event.mouse.x);
  578.       printf("  y        = %i,\n",        event.mouse.y);
  579.       printf("  dx       = %i,\n",        event.mouse.dx);
  580.       printf("  dy       = %i,\n",        event.mouse.dy);
  581.       printf("  data     = %i,\n",        event.mouse.data);
  582.       printf("  button   = 0x%02X,\n",    event.mouse.button);
  583.       printf("  pressed  = %s,\n",getBool(event.mouse.pressed));
  584.       printf("  dblClick = %s,\n",getBool(event.mouse.dblClick));
  585.     } break;
  586.  
  587.     default:;
  588.   }
  589.  
  590.   printf("}\n");
  591. }
  592.  
  593.  
  594.  
  595.  
  596. #endif /* TEST_MAIN_UTILS_HPP */
  597. /******************************************************************************/
  598. /******************************************************************************/
  599. //"kit_w32\src\kit_win32\kit_Bitmap_shared.hpp":
  600. #ifndef _KIT_SRC_KIT_BITMAP_SHARED_HPP
  601. #define _KIT_SRC_KIT_BITMAP_SHARED_HPP
  602.  
  603. #include "_kit_common_shared.hpp"
  604.  
  605.  
  606. #define KIT_MAGIC_QOI (0x66696F71u) //="qoif"
  607.  
  608.  
  609. namespace kit {
  610.  
  611.  
  612.  
  613.  
  614. //returns true on success
  615. static inline bool ResizeBitmapOpaque(_BitmapOpaque& bmp,
  616.                                       s32 width, s32 height)
  617. {
  618.   //set dimensions of bitmap to new values
  619.   bmp.size.x = width;
  620.   bmp.size.y = height; //working with shape::point is a lot prettier than...
  621.   bmp.info.bmiHeader.biWidth  = width; //...whatever this is
  622.   bmp.info.bmiHeader.biHeight = height;
  623.  
  624.  
  625.   //delete old and create new device context
  626.   if(bmp.devCtx != nullptr) DeleteDC(bmp.devCtx);
  627.  
  628.   HDC clientAreaDC = GetDC(bmp.window->winHandle);
  629.   bmp.devCtx = CreateCompatibleDC(clientAreaDC);
  630.   if(bmp.devCtx == nullptr){
  631.     ReleaseDC(bmp.window->winHandle, clientAreaDC);
  632.     return false;
  633.   }
  634.  
  635.  
  636.   //delete old bitmap
  637.   if(bmp.handle != nullptr) DeleteObject(bmp.handle);
  638.  
  639.  
  640.   //create new bitmap
  641.   if(bmp.directAccess){
  642.     bmp.handle = CreateDIBSection(clientAreaDC, &bmp.info, DIB_RGB_COLORS,
  643.                                   (void**)&bmp.pixels, nullptr, 0);
  644.  
  645.   } else {
  646.     bmp.handle = CreateCompatibleBitmap(clientAreaDC, width,height);
  647.   }
  648.  
  649.  
  650.   //release window dc resources
  651.   ReleaseDC(bmp.window->winHandle, clientAreaDC);
  652.  
  653.  
  654.   //bind device context to new bitmap, and set stretch mode
  655.   SelectObject(bmp.devCtx, bmp.handle);
  656.   //(currently, this only materially affects a Window's canvas bitmap)
  657.   SetStretchBltMode(bmp.devCtx, bmp.stretchMode);
  658.  
  659.   return true; //returns only true for now
  660. }
  661.  
  662.  
  663.  
  664.  
  665. //returns true on success
  666. static inline bool PopulateBitmapOpaque(_BitmapOpaque& bmp,
  667.                                         const color::ARGB* pixelData,
  668.                                         s32 width, s32 height,
  669.                                         _WindowOpaque* blit_dst,
  670.                                         bool directAccess)
  671. {
  672.   //fill in internal bitmap info
  673.   bmp.info.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
  674.   bmp.info.bmiHeader.biWidth       = width;
  675.   bmp.info.bmiHeader.biHeight      = height;
  676.   bmp.info.bmiHeader.biPlanes      = 1;      //should always be 1
  677.   bmp.info.bmiHeader.biBitCount    = 32;     //for 0x--RRGGBB
  678.   bmp.info.bmiHeader.biCompression = BI_RGB; //uncompressed rgb
  679.  
  680.   bmp.size.x = width;
  681.   bmp.size.y = height;
  682.  
  683.   bmp.window = blit_dst;
  684.  
  685.   //create the bitmap's device context (for rendering directly to the bitmap)
  686.   bmp.stretchMode  = KIT_INITIAL_STRETCH_MODE;
  687.  
  688.   //determines whether you can edit the bitmap color data directly
  689.   bmp.directAccess = directAccess;
  690.  
  691.  
  692.   HDC clientAreaDC = GetDC(blit_dst->winHandle);
  693.   bmp.devCtx = CreateCompatibleDC(clientAreaDC);
  694.   if(bmp.devCtx == nullptr){
  695.     ReleaseDC(blit_dst->winHandle, clientAreaDC);
  696.     return false;
  697.   }
  698.  
  699.  
  700.   if(directAccess){
  701.     bmp.handle = CreateDIBSection(clientAreaDC, &bmp.info, DIB_RGB_COLORS,
  702.                                   (void**)&bmp.pixels, nullptr, 0);
  703.  
  704.   } else {
  705.     bmp.handle = CreateCompatibleBitmap(clientAreaDC, width, height);
  706.  
  707.   }
  708.  
  709.  
  710.   //release window dc resources
  711.   ReleaseDC(blit_dst->winHandle, clientAreaDC);
  712.  
  713.  
  714.   //bind device context to new bitmap, and set stretch mode
  715.   if(bmp.handle != nullptr){
  716.     SelectObject(bmp.devCtx, bmp.handle);
  717.     //(currently, this only materially affects a Window's canvas bitmap)
  718.     SetStretchBltMode(bmp.devCtx, KIT_INITIAL_STRETCH_MODE);
  719.     return true;
  720.   } else {
  721.     DeleteDC(bmp.devCtx);
  722.     bmp.devCtx = nullptr;
  723.     return false;
  724.   }
  725. }
  726.  
  727.  
  728.  
  729. }; /* namespace kit */
  730.  
  731. #endif /* _KIT_SRC_KIT_BITMAP_SHARED_HPP */
  732. /******************************************************************************/
  733. /******************************************************************************/
  734. //"kit_w32\src\kit_win32\kit_fileio_shared.hpp":
  735. #ifndef _KIT_SRC_KIT_FILEIO_SHARED_HPP
  736. #define _KIT_SRC_KIT_FILEIO_SHARED_HPP
  737.  
  738. #include "_kit_common_shared.hpp"
  739.  
  740.  
  741. namespace kit {
  742.  
  743.  
  744. namespace fileio {
  745.  
  746.  
  747.  
  748.  
  749. //nothing to put here (yet)
  750.  
  751.  
  752.  
  753.  
  754. }; /* namespace fileio */
  755.  
  756.  
  757. }; /* namespace kit */
  758.  
  759. #endif /* _KIT_SRC_KIT_FILEIO_SHARED_HPP */
  760. /******************************************************************************/
  761. /******************************************************************************/
  762. //"kit_w32\src\kit_win32\kit_Window_shared.hpp":
  763. #ifndef _KIT_SRC_KIT_WINDOW_SHARED_HPP
  764. #define _KIT_SRC_KIT_WINDOW_SHARED_HPP
  765.  
  766. #include "_kit_common_shared.hpp"
  767.  
  768.  
  769. namespace kit {
  770.  
  771.  
  772.  
  773.  
  774. /*+++++++++++++++++++++++*/
  775. /*+kit_Window_WindowProc+*/
  776. /*+++++++++++++++++++++++*/
  777.  
  778. namespace w32 {
  779.  
  780.  
  781.  
  782. extern LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
  783.  
  784.  
  785.  
  786. };
  787.  
  788. /*-----------------------*/
  789. /*-kit_Window_WindowProc-*/
  790. /*-----------------------*/
  791.  
  792.  
  793.  
  794.  
  795. static inline shape::rect ConvertToKitRect(RECT& rectIn){
  796.   shape::rect rectOut;
  797.   rectOut.x = rectIn.left;
  798.   rectOut.y = rectIn.top;
  799.   rectOut.w = rectIn.right  - rectIn.left;
  800.   rectOut.h = rectIn.bottom - rectIn.top;
  801.   return rectOut;
  802. }
  803.  
  804.  
  805.  
  806. static inline RECT ConvertFromKitRect(shape::rect& rectIn){
  807.   RECT rectOut;
  808.   rectOut.left   = rectIn.x;
  809.   rectOut.top    = rectIn.y;
  810.   rectOut.right  = rectIn.x + rectIn.w;
  811.   rectOut.bottom = rectIn.y + rectIn.h;
  812.   return rectOut;
  813. }
  814.  
  815.  
  816.  
  817.  
  818. //assumes window is without a menu
  819. static inline shape::point CalculateWindowSize(u32 innerWidth, u32 innerHeight,
  820.                                                u32 flags,      u32 flagsEx)
  821. {
  822.   RECT winSize;
  823.   winSize.left   = 0;
  824.   winSize.top    = 0;
  825.   winSize.right  = innerWidth;
  826.   winSize.bottom = innerHeight;
  827.   AdjustWindowRectEx(&winSize, flags, false, flagsEx);
  828.  
  829.   shape::point winSizeAdjusted;
  830.   winSizeAdjusted.x = winSize.right  - winSize.left;
  831.   winSizeAdjusted.y = winSize.bottom - winSize.top;
  832.   return winSizeAdjusted;
  833. }
  834.  
  835.  
  836.  
  837.  
  838. static inline void HandlePreCreationWindowFlags(_WindowOpaque* opq,
  839.                                                 u32 kitWinFlags)
  840. {
  841.   //pre-creation handling of some window flags
  842.  
  843.   opq->winFlags   = WS_OVERLAPPEDWINDOW; //will be hidden initially
  844.   opq->winFlagsEx = 0;
  845.  
  846.   if(kitWinFlags & WINFLAG_BORDERLESS){
  847.     kitWinFlags &= ~WINFLAG_RESIZABLE;
  848.     opq->winFlags &= WS_SYSMENU;
  849.   }
  850.  
  851.   if(!(kitWinFlags & WINFLAG_RESIZABLE)) opq->winFlags &= ~WS_THICKFRAME;
  852.  
  853.   if(kitWinFlags & WINFLAG_REM_MINIMIZE) opq->winFlags &= ~WS_MINIMIZEBOX;
  854.   if(kitWinFlags & WINFLAG_REM_MAXIMIZE) opq->winFlags &= ~WS_MAXIMIZEBOX;
  855. }
  856.  
  857.  
  858.  
  859.  
  860. };
  861.  
  862. #endif /* _KIT_SRC_KIT_WINDOW_SHARED_HPP */
  863. /******************************************************************************/
  864. /******************************************************************************/
  865. //"kit_w32\src\kit_win32\kit_Window_WindowProcEvent.hpp":
  866. #ifndef _KIT_SRC_KIT_WINDOW_WINDOWPROCEVENT_HPP
  867. #define _KIT_SRC_KIT_WINDOW_WINDOWPROCEVENT_HPP
  868.  
  869. #include <kit/_video_WindowEvent.hpp>
  870. #include "_kit_globals.hpp"
  871.  
  872. //(the WindowEvent used inside WindowProc is named "event")
  873. #define KIT_HANDLE_EVENT(_name,...) \
  874.   kit::WindowProcEvent::_name(event,__VA_ARGS__)
  875.  
  876.  
  877. namespace kit {
  878.  
  879.  
  880. namespace WindowProcEvent {
  881.  
  882.  
  883.  
  884.  
  885. /*+WINEVENT_WIN+*/
  886.  
  887. //triggered when a window requests to be closed
  888. static inline void WIN_CLOSE(WindowEvent& event){
  889.   event.type = WINEVENT_WIN_CLOSE;
  890. }
  891.  
  892.  
  893.  
  894. static inline void WIN_MOVED(WindowEvent& event, shape::rect& newRect){
  895.   event.type = WINEVENT_WIN_MOVED;
  896.   //(dataX&Y are unioned with data1&2)
  897.   event.win.dataX = newRect.x; //new horizontal position
  898.   event.win.dataY = newRect.y; //new vertical position
  899. }
  900.  
  901.  
  902. static inline void WIN_RESIZED(WindowEvent& event, shape::rect& newRect){
  903.   event.type = WINEVENT_WIN_RESIZED;
  904.   //(dataX&Y are unioned with data1&2)
  905.   event.win.dataX = newRect.w; //new width
  906.   event.win.dataY = newRect.h; //new height
  907. }
  908.  
  909.  
  910.  
  911. static inline void WIN_UNFOCUSING(WindowEvent& event){
  912.   event.type = WINEVENT_WIN_UNFOCUSING;
  913. }
  914.  
  915.  
  916. static inline void WIN_FOCUSED(WindowEvent& event){
  917.   event.type = WINEVENT_WIN_FOCUSED;
  918. }
  919.  
  920.  
  921.  
  922. static inline void WIN_MINIMIZED(WindowEvent& event){
  923.   event.type = WINEVENT_WIN_MINIMIZED;
  924. }
  925.  
  926.  
  927. static inline void WIN_MAXIMIZED(WindowEvent& event){
  928.   event.type = WINEVENT_WIN_MAXIMIZED;
  929. }
  930.  
  931.  
  932. static inline void WIN_RESTORED(WindowEvent& event){
  933.   event.type = WINEVENT_WIN_RESTORED;
  934. }
  935.  
  936. /*-WINEVENT_WIN-*/
  937.  
  938.  
  939.  
  940.  
  941. /*+WINEVENT_KEY+*/
  942.  
  943. union KEY_Params {
  944.   struct {
  945.     u32 repeatCount   : 16; // 0 -> 15
  946.     u32 scanCode      :  8; //16 -> 23
  947.     u32 extendedKey   :  1; //24
  948.     u32 _reserved     :  4; //25 -> 28
  949.     u32 altKeyDown    :  1; //29
  950.     u32 prevUnpressed :  1; //30
  951.     u32 currUnpressed :  1; //31
  952.   };
  953.   u32 value;
  954.   KEY_Params(u32 _value) : value(_value) {}
  955. };
  956.  
  957.  
  958.  
  959. //this event handler is used for KEY_CHAR, KEY_UP, and KEY_DOWN events
  960. static inline void KEY_CHARUPDOWN(WindowEvent& event,
  961.                                   bool charEvent,     u8 virtualKeyCode,
  962.                                   KEY_Params& params, u16 kmods)
  963. {
  964.   if(charEvent){
  965.     event.type = WINEVENT_KEY_CHAR;
  966.   } else {
  967.     if(params.currUnpressed) event.type = WINEVENT_KEY_UP;
  968.     else                     event.type = WINEVENT_KEY_DOWN;
  969.   }
  970.  
  971.   event.key.kmods = kmods;
  972.  
  973.   event.key.pkey = params.scanCode;
  974.   event.key.vkey = virtualKeyCode;
  975.  
  976.   event.key.pressed = !params.currUnpressed;
  977.   event.key.repeat  = params.repeatCount>0; //modified to act as a boolean
  978.   event.key.ischar  = charEvent;
  979. }
  980.  
  981. /*-WINEVENT_KEY-*/
  982.  
  983.  
  984.  
  985.  
  986. /*+WINEVENT_MOUSE+*/
  987.  
  988. union MOUSE_ButtonStates {
  989.   struct {
  990.     u8 left    : 1;
  991.     u8 middle  : 1;
  992.     u8 right   : 1;
  993.     u8 x1      : 1;
  994.     u8 x2      : 1;
  995.     u8 _unused : 3;
  996.   };
  997.   u8 value;
  998.   MOUSE_ButtonStates() : value(0) {}
  999.   MOUSE_ButtonStates(u8 _value) : value(_value) {}
  1000. };
  1001.  
  1002.  
  1003.  
  1004. //MOUSE_MOVED was already taken by a win32 macro >:(
  1005. static inline void MOUSE_MOVE(WindowEvent& event, u8 buttonStates,
  1006.                               shape::point& previous, shape::point& current)
  1007. {
  1008.   event.type = WINEVENT_MOUSE_MOVE;
  1009.  
  1010.   event.mouse.x = current.x;
  1011.   event.mouse.y = current.y;
  1012.  
  1013.   event.mouse.dx = current.x - previous.x;
  1014.   event.mouse.dy = current.y - previous.y;
  1015.  
  1016.   event.mouse.button  = buttonStates;
  1017.   event.mouse.pressed = buttonStates!=0;
  1018. }
  1019.  
  1020.  
  1021. static inline void MOUSE_LEAVE(WindowEvent& event){
  1022.   event.type = WINEVENT_MOUSE_LEAVE;
  1023. }
  1024.  
  1025.  
  1026. static inline void MOUSE_ENTER(WindowEvent& event){
  1027.   event.type = WINEVENT_MOUSE_ENTER;
  1028. }
  1029.  
  1030.  
  1031. //same event handler is used for both MOUSE_UP and MOUSE_DOWN events
  1032. static inline void MOUSE_UPDOWN(WindowEvent& event, shape::point& clickPosition,
  1033.                                 u8 buttonStates, bool pressed, bool doubleClick)
  1034. {
  1035.   if(pressed) event.type = WINEVENT_MOUSE_DOWN;
  1036.   else        event.type = WINEVENT_MOUSE_UP;
  1037.  
  1038.   event.mouse.x = clickPosition.x;
  1039.   event.mouse.y = clickPosition.y;
  1040.  
  1041.   event.mouse.button   = buttonStates;
  1042.   event.mouse.pressed  = pressed;
  1043.   event.mouse.dblClick = doubleClick;
  1044. }
  1045.  
  1046.  
  1047. //same event handler is used for both MOUSE_HWHEEL and MOUSE_VWHEEL events
  1048. static inline void MOUSE_HVWHEEL(WindowEvent& event,
  1049.                                  bool verticalScroll, s16 scrollAmount,
  1050.                                  u8 buttonStates, shape::point& mousePos)
  1051. {
  1052.   if(verticalScroll){
  1053.     event.type = WINEVENT_MOUSE_VWHEEL;
  1054.     event.mouse.dy = scrollAmount/WHEEL_DELTA;
  1055.   } else {
  1056.     event.type = WINEVENT_MOUSE_HWHEEL;
  1057.     event.mouse.dx = scrollAmount/WHEEL_DELTA;
  1058.   }
  1059.  
  1060.   event.mouse.x = mousePos.x;
  1061.   event.mouse.y = mousePos.y;
  1062.  
  1063.   event.mouse.data = scrollAmount; //the scroll value before delta is applied
  1064.  
  1065.   event.mouse.button  = buttonStates;
  1066.   event.mouse.pressed = buttonStates!=0;
  1067. }
  1068.  
  1069. /*-WINEVENT_MOUSE-*/
  1070.  
  1071.  
  1072.  
  1073.  
  1074. }; /* WindowProcEvent */
  1075.  
  1076.  
  1077. }; /* namespace kit */
  1078.  
  1079. #endif /* _KIT_SRC_KIT_WINDOW_WINDOWPROCEVENT_HPP */
  1080.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement