1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #pragma once
19 
20 #include <functional>
21 #include "CommonWindows.h"
22 
23 #include "Common/GPU/OpenGL/GLSLProgram.h"
24 #include "Common/CommonWindows.h"
25 #include "Windows/W32Util/ContextMenu.h"
26 
27 struct SimpleGLWindow {
28 	static const wchar_t *windowClass;
29 
30 	enum Format {
31 		FORMAT_565_REV = 0x00,
32 		FORMAT_5551_REV = 0x01,
33 		FORMAT_4444_REV = 0x02,
34 		FORMAT_8888 = 0x03,
35 		FORMAT_565 = 0x04,
36 		FORMAT_5551 = 0x05,
37 		FORMAT_4444 = 0x06,
38 		FORMAT_5551_BGRA_REV = 0x09,
39 		FORMAT_4444_BGRA_REV = 0x0A,
40 		FORMAT_8888_BGRA = 0x0B,
41 
42 		FORMAT_FLOAT = 0x10,
43 		FORMAT_16BIT = 0x11,
44 		FORMAT_8BIT = 0x12,
45 		FORMAT_24BIT_8X = 0x13,
46 		FORMAT_24X_8BIT = 0x14,
47 
48 		FORMAT_FLOAT_DIV_256 = 0x18,
49 		FORMAT_24BIT_8X_DIV_256 = 0x1B,
50 	};
51 
52 	enum Flags {
53 		RESIZE_NONE = 0x00,
54 		RESIZE_CENTER = 0x01,
55 		RESIZE_SHRINK_FIT = 0x02,
56 		RESIZE_SHRINK_CENTER = 0x03,
57 		RESIZE_GROW_FIT = 0x04,
58 		RESIZE_GROW_CENTER = 0x05,
59 		RESIZE_BEST_FIT = 0x06,
60 		RESIZE_BEST_CENTER = 0x07,
61 
62 		ALPHA_IGNORE = 0x00,
63 		ALPHA_BLEND = 0x08,
64 	};
65 
66 	SimpleGLWindow(HWND wnd);
67 	~SimpleGLWindow();
68 
69 	void Clear();
70 	void Draw(const u8 *data, int w, int h, bool flipped = false, Format = FORMAT_8888);
71 	void Redraw(bool andSwap = true);
72 	void Initialize(u32 flags);
73 	static SimpleGLWindow *GetFrom(HWND hwnd);
74 	static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
75 
76 	// To draw something custom.
77 	void Begin();
78 	void End();
79 
SetFlagsSimpleGLWindow80 	void SetFlags(u32 flags) {
81 		flags_ = flags;
82 	}
83 
SwapSimpleGLWindow84 	void Swap() {
85 		swapped_ = true;
86 		SwapBuffers(hDC_);
87 	}
88 
WidthSimpleGLWindow89 	int Width() {
90 		return w_;
91 	}
92 
HeightSimpleGLWindow93 	int Height() {
94 		return h_;
95 	}
96 
HasTexSimpleGLWindow97 	bool HasTex() {
98 		return tw_ > 0 && th_ > 0;
99 	}
100 
TexWidthSimpleGLWindow101 	int TexWidth() {
102 		return tw_;
103 	}
104 
TexHeightSimpleGLWindow105 	int TexHeight() {
106 		return th_;
107 	}
108 
109 	void GetContentSize(float &x, float &y, float &fw, float &fh);
110 
SetRedrawCallbackSimpleGLWindow111 	void SetRedrawCallback(std::function<void()> callback) {
112 		redrawCallback_ = callback;
113 	}
114 
SetHoverCallbackSimpleGLWindow115 	void SetHoverCallback(std::function<void(int, int)> hoverCallback) {
116 		hoverCallback_ = hoverCallback;
117 	}
118 
119 	// Called first with 0 that it's opening, then the selected item.
SetRightClickMenuSimpleGLWindow120 	void SetRightClickMenu(ContextMenuID menu, std::function<void(int)> callback) {
121 		rightClickCallback_ = callback;
122 		rightClickMenu_ = menu;
123 	}
124 
125 	static void RegisterClass();
126 protected:
127 	void SetupGL();
128 	void ResizeGL(int w, int h);
129 	void CreateProgram();
130 	void GenerateChecker();
131 	void DrawChecker();
132 	bool DragStart(int mouseX, int mouseY);
133 	bool DragContinue(int mouseX, int mouseY);
134 	bool DragEnd(int mouseX, int mouseY);
135 	bool Hover(int mouseX, int mouseY);
136 	bool Leave();
137 	bool RightClick(int mouseX, int mouseY);
138 	bool ToggleZoom();
139 	const u8 *Reformat(const u8 *data, Format fmt, u32 numPixels);
140 
141 	HWND hWnd_;
142 	HDC hDC_;
143 	HGLRC hGLRC_;
144 	bool valid_ = false;
145 	// Width and height of the window.
146 	int w_;
147 	int h_;
148 	// Last texture size/flipped for Redraw().
149 	int tw_;
150 	int th_;
151 	bool tflipped_;
152 
153 	GLSLProgram *drawProgram_ = nullptr;
154 	GLuint vao_ = 0;
155 	GLuint ibuf_ = 0;
156 	GLuint vbuf_ = 0;
157 	GLuint checker_ = 0;
158 	GLuint tex_ = 0;
159 	u32 flags_ = 0;
160 	// Disable shrink (toggled by double click.)
161 	bool zoom_ = false;
162 	bool dragging_ = false;
163 	bool inRedrawCallback_ = false;
164 	bool swapped_ = false;
165 	int dragStartX_;
166 	int dragStartY_;
167 	u32 dragLastUpdate_;
168 	// Offset to position the texture is drawn at.
169 	int offsetX_ = 0;
170 	int offsetY_ = 0;
171 	u32 *reformatBuf_ = nullptr;
172 	u32 reformatBufSize_ = 0;
173 
174 	std::function<void()> redrawCallback_;
175 	std::function<void(int, int)> hoverCallback_;
176 	std::function<void(int)> rightClickCallback_;
177 	ContextMenuID rightClickMenu_;
178 };
179