Skip to content

Commit 6bea8ed

Browse files
committed
1 parent b254de4 commit 6bea8ed

File tree

9 files changed

+85
-40
lines changed

9 files changed

+85
-40
lines changed

include/application-base/applicationWin32.h

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11

2-
enum MouseButtonState
2+
enum MouseButtonType
33
{
4-
MouseButtonNone,
54
MouseButtonLeft,
65
MouseButtonRight,
6+
MouseButtonCount
7+
};
8+
9+
struct MouseState
10+
{
11+
MouseState()
12+
{
13+
mouseWheel = 0;
14+
buttons[MouseButtonLeft] = false;
15+
buttons[MouseButtonRight] = false;
16+
}
17+
vec2i pos;
18+
int mouseWheel;
19+
bool buttons[MouseButtonCount];
720
};
821

922
struct InputState
@@ -12,18 +25,13 @@ struct InputState
1225
InputState()
1326
{
1427
for(bool &b : keys) b = false;
15-
mouseX = mouseY = prevMouseX = prevMouseY = 0;
16-
mouseState = prevMouseState = MouseButtonNone;
1728
}
1829

1930
static const UINT keyCount = 256;
2031
bool keys[keyCount];
2132

22-
int mouseX, mouseY;
23-
MouseButtonState mouseState;
24-
25-
int prevMouseX, prevMouseY;
26-
MouseButtonState prevMouseState;
33+
MouseState mouse;
34+
MouseState prevMouse;
2735
};
2836

2937
struct ApplicationData
@@ -47,7 +55,9 @@ class ApplicationCallback
4755
virtual void render(ApplicationData &app) = 0;
4856
virtual void keyDown(ApplicationData &app, UINT key) = 0;
4957
virtual void keyPressed(ApplicationData &app, UINT key) = 0;
50-
virtual void mouse(ApplicationData &app, int x, int y, int prevX, int prevY) = 0;
58+
virtual void mouseDown(ApplicationData &app, MouseButtonType button) = 0;
59+
virtual void mouseMove(ApplicationData &app) = 0;
60+
virtual void resize(ApplicationData &app) = 0;
5161
};
5262

5363
class ApplicationWin32
@@ -66,12 +76,17 @@ class ApplicationWin32
6676
{
6777
return m_callback;
6878
}
79+
inline bool initialized()
80+
{
81+
return m_initialized;
82+
}
6983

7084
private:
7185
//
7286
// m_data is just a view to encapsulate all externally-visible application
7387
// components. THe actual data is owned by m_window, m_device, etc.
7488
//
89+
bool m_initialized;
7590
ApplicationData *m_data;
7691

7792
WindowWin32 m_window;

include/core-graphics/camera.h

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Camera
66
Camera() {}
77
Camera(const vec3f &eye, const vec3f &worldUp, const vec3f &right, float fieldOfView, float aspect, float zNear, float zFar);
88

9+
void updateAspectRatio(float newAspect);
910
void lookRight(float theta);
1011
void lookUp(float theta);
1112
void roll(float theta);
@@ -40,4 +41,6 @@ class Camera
4041
mat4f m_camera;
4142
mat4f m_perspective;
4243
mat4f m_cameraPerspective;
44+
45+
float m_fieldOfView, m_aspect, m_zNear, m_zFar;
4346
};

src/application-base/applicationWin32.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ApplicationWin32::ApplicationWin32(HINSTANCE instance, UINT windowWidth, UINT wi
33
m_callback(callback),
44
m_window(*this)
55
{
6+
m_initialized = false;
67
m_window.init(instance, windowWidth, windowHeight, name);
78

89
switch(graphicsType)
@@ -33,6 +34,8 @@ void ApplicationWin32::messageLoop()
3334
msg.message = WM_NULL;
3435
PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
3536

37+
m_initialized = true;
38+
3639
while( WM_QUIT != msg.message )
3740
{
3841
// Use PeekMessage() so we can use idle time to render the scene.

src/application-base/windowWin32.cpp

+23-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ WindowWin32* s_mainWindow = NULL;
77

88
LRESULT WINAPI WindowCallback( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
99
{
10-
if(s_mainWindow == NULL) return DefWindowProc( hWnd, msg, wParam, lParam );
10+
if(s_mainWindow == NULL || !s_mainWindow->parent().initialized()) return DefWindowProc( hWnd, msg, wParam, lParam );
11+
12+
auto &parent = s_mainWindow->parent();
13+
1114
switch( msg )
1215
{
1316
case WM_SYSCOMMAND:
@@ -33,52 +36,49 @@ LRESULT WINAPI WindowCallback( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam
3336
break;
3437
default:
3538
UINT keyIndex = (UINT)wParam;
36-
s_mainWindow->parent().callback().keyDown(s_mainWindow->parent().data(), (UINT)wParam);
37-
if(keyIndex < InputState::keyCount) s_mainWindow->parent().data().input.keys[keyIndex] = true;
39+
parent.callback().keyDown(parent.data(), (UINT)wParam);
40+
if(keyIndex < InputState::keyCount) parent.data().input.keys[keyIndex] = true;
3841
break;
3942
}
4043
break;
4144

4245
case WM_KEYUP:
43-
UINT keyIndex = (UINT)wParam;
44-
if(keyIndex < InputState::keyCount) s_mainWindow->parent().data().input.keys[keyIndex] = false;
46+
{
47+
UINT keyIndex = (UINT)wParam;
48+
if(keyIndex < InputState::keyCount) parent.data().input.keys[keyIndex] = false;
49+
}
4550
break;
4651

47-
/*case WM_LBUTTONDOWN:
48-
g_WndProcContext->SetMouseState(MouseButtonLeft, true);
52+
case WM_SIZE:
53+
parent.callback().resize(parent.data());
54+
break;
55+
56+
case WM_LBUTTONDOWN:
57+
parent.data().input.mouse.buttons[MouseButtonLeft] = true;
4958
break;
5059

5160
case WM_LBUTTONUP:
52-
g_WndProcContext->SetMouseState(MouseButtonLeft, false);
61+
parent.data().input.mouse.buttons[MouseButtonLeft] = false;
5362
break;
5463

5564
case WM_RBUTTONDOWN:
56-
g_WndProcContext->SetMouseState(MouseButtonRight, true);
65+
parent.data().input.mouse.buttons[MouseButtonRight] = true;
5766
break;
5867

5968
case WM_RBUTTONUP:
60-
g_WndProcContext->SetMouseState(MouseButtonRight, false);
61-
break;
62-
63-
case WM_MBUTTONDOWN:
64-
g_WndProcContext->SetMouseState(MouseButtonMiddle, true);
65-
break;
66-
67-
case WM_MBUTTONUP:
68-
g_WndProcContext->SetMouseState(MouseButtonMiddle, false);
69+
parent.data().input.mouse.buttons[MouseButtonRight] = false;
6970
break;
7071

7172
case WM_MOUSEMOVE:
7273
{
73-
POINTS P = MAKEPOINTS(lParam);
74-
Vec2i NewPos(P.x, P.y);
75-
g_WndProcContext->UpdateMousePos(NewPos);
74+
POINTS p = MAKEPOINTS(lParam);
75+
parent.data().input.mouse.pos = vec2i(p.x, p.y);
7676
}
7777
break;
7878

7979
case WM_MOUSEWHEEL:
80-
g_WndProcContext->UpdateWheelState(GET_WHEEL_DELTA_WPARAM(wParam));
81-
break;*/
80+
parent.data().input.mouse.mouseWheel += GET_WHEEL_DELTA_WPARAM(wParam);
81+
break;
8282
}
8383

8484
return DefWindowProc( hWnd, msg, wParam, lParam );

src/application-d3d11/D3D11GraphicsDevice.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ void D3D11GraphicsDevice::renderBeginFrame()
8686

8787
void D3D11GraphicsDevice::renderEndFrame()
8888
{
89-
m_swapChain->Present( 0, 0 );
89+
m_swapChain->Present( 1, 0 );
9090
}

src/core-graphics/camera.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@ Camera::Camera(const vec3f &eye, const vec3f &worldUp, const vec3f &right, float
77
m_look = (m_worldUp ^ m_right).normalize();
88
m_up = (m_right ^ m_look).normalize();
99

10-
m_perspective = perspectiveFov(fieldOfView, aspect, zNear, zFar);
10+
m_fieldOfView = fieldOfView;
11+
m_aspect = aspect;
12+
m_zNear = zNear;
13+
m_zFar = zFar;
14+
15+
m_perspective = perspectiveFov(m_fieldOfView, m_aspect, m_zNear, m_zFar);
1116

1217
update();
1318
}
1419

20+
void Camera::updateAspectRatio(float newAspect)
21+
{
22+
m_aspect = newAspect;
23+
m_perspective = perspectiveFov(m_fieldOfView, m_aspect, m_zNear, m_zFar);
24+
}
25+
1526
void Camera::update()
1627
{
1728
m_camera = viewMatrix(m_eye, m_look, m_up, m_right);

testD3D11/appTest.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void AppTest::init(ApplicationData &app)
1313
//vec3f eye(1.0f, 2.0f, 3.0f);
1414
vec3f eye(0.0f, 0.0f, 0.0f);
1515
vec3f worldUp(0.0f, 0.0f, 1.0f);
16-
m_camera = Camera(eye, worldUp, worldUp ^ (vec3f::eX - eye), 60.0f, (float)app.window.height() / app.window.width(), 0.01f, 1000.0f);
16+
m_camera = Camera(eye, worldUp, worldUp ^ (vec3f::eX - eye), 60.0f, (float)app.window.width() / app.window.height(), 0.01f, 1000.0f);
1717
}
1818

1919
void AppTest::render(ApplicationData &app)
@@ -29,6 +29,11 @@ void AppTest::render(ApplicationData &app)
2929
m_mesh.render(app.graphics);
3030
}
3131

32+
void AppTest::resize(ApplicationData &app)
33+
{
34+
m_camera.updateAspectRatio((float)app.window.width() / app.window.height());
35+
}
36+
3237
void AppTest::keyDown(ApplicationData &app, UINT key)
3338
{
3439

@@ -50,7 +55,12 @@ void AppTest::keyPressed(ApplicationData &app, UINT key)
5055
if(key == KEY_RIGHT) m_camera.lookRight(-theta);
5156
}
5257

53-
void AppTest::mouse(ApplicationData &app, int x, int y, int prevX, int prevY)
58+
void AppTest::mouseDown(ApplicationData &app, MouseButtonType button)
59+
{
60+
61+
}
62+
63+
void AppTest::mouseMove(ApplicationData &app)
5464
{
5565

56-
}
66+
}

testD3D11/appTest.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class AppTest : public ApplicationCallback
1111
void render(ApplicationData &app);
1212
void keyDown(ApplicationData &app, UINT key);
1313
void keyPressed(ApplicationData &app, UINT key);
14-
void mouse(ApplicationData &app, int x, int y, int prevX, int prevY);
14+
void mouseDown(ApplicationData &app, MouseButtonType button);
15+
void mouseMove(ApplicationData &app);
16+
void resize(ApplicationData &app);
1517

1618
private:
1719
D3D11TriMesh m_mesh;

testD3D11/shaders/test.shader

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ cbuffer ConstantBuffer : register( b0 )
77
struct VertexShaderOutput
88
{
99
float4 pos : SV_POSITION;
10+
float2 tex : TEXCOORD0;
1011
};
1112

12-
VertexShaderOutput vertexShaderMain( float4 pos : position ) : SV_POSITION
13+
VertexShaderOutput vertexShaderMain( float4 pos : position )
1314
{
1415
VertexShaderOutput output;
1516
output.pos = mul( pos, worldViewProj );

0 commit comments

Comments
 (0)