------

[ AD ] Port Monitor ( Try to use a Best WebSite Monitoring Tool )

------

2D Game Engine 만들기 – tutorial with SDL

SDL Events

Tim Jones | October 25th, 2007

 

Alongside the basics of game development is something called Events.
All videogames, from pong to the highly complex PC games and console titles, use events to interact with the player.
These events can come from keyboards, mice, joysticks, gamepads, and so on, or events from our operating system.
It's important to understand how events work if we are to appropriately interact a user with a game.
We've already been using events, but only for closing our window, now we'll look at how to receive events from the user.


게임 개발의 기초 옆에는 이벤트라는 것이 있습니다.
모든 비디오 게임은 탁구게임부터 매우 복잡한 PC 게임 및 콘솔 타이틀까지, 플레이어와 상호 작용하기 위해 이벤트를 사용합니다.
이러한 이벤트는 키보드, 마우스, 조이스틱, 게임 패드 등, 또는 운영 체제에서 이벤트 옵니다.
우리가 적절하게 게임 사용자를 상호 작용하기위해, 이벤트의 작동 방법을 이해하는 것이 중요합니다.
우리는 이미 이벤트를 사용하고 있었어, 하지만 우리의 창문을 폐쇄, 이제는 사용자로부터 이벤트를 받는 방법을 보겠습니다.


If you haven't caught on, each tutorial builds off the previous, so we'll be using the code from the previous lesson.
In order to track all these events and throw them into nice functions, lets create a new class.

 

Create two files called CEvent.h and CEvent.cpp.
These two files will handle our events, and call the appropriate function.

CApp will inherit this class,
so when we need to catch an event we simply override a function.
당신이 잡혀있지 않았다면,  튜토리얼은 이전 빌드에서,그래서 우리가 이전 강의 코드를 사용합니다.
이러한 모든 이벤트를 추적하고, 좋은 기능으로 그들에게 던져주기 위해 새로운 클래스를 만든다.

 

 CEvent.h  CEvent.cpp라는  개의 파일 만듭니다. 
 파일은 (우리의) 이벤트를 처리하고, 적절한 함수를 호출합니다. 

CApp  클래스를 상속하게 되고, 
(
우리가) 이벤트를  잡아야 할때 (우리는) 단순히 기능을 재정의합니다.


Open up CEvent.h and add the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#ifndef _CEVENT_H_

#define _CEVENT_H_

#include <SDL.h>

class CEvent

{

public:

CEvent();

virtual ~CEvent();

virtual void OnEvent(SDL_Event* Event);

virtual void OnInputFocus();

virtual void OnInputBlur();

// Key

virtual void OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode);

virtual void OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode);

// Mouse

virtual void OnMouseFocus();

virtual void OnMouseBlur();

virtual void OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle);

 

virtual void OnMouseWheel(bool Up, bool Down); //Not implemented

// Button

virtual void OnLButtonDown(int mX, int mY);

virtual void OnLButtonUp(int mX, int mY);

virtual void OnRButtonDown(int mX, int mY);

virtual void OnRButtonUp(int mX, int mY);

virtual void OnMButtonDown(int mX, int mY);

virtual void OnMButtonUp(int mX, int mY);

// Joy

virtual void OnJoyAxis(Uint8 which, Uint8 axis, Sint16 value);

virtual void OnJoyButtonDown(Uint8 which, Uint8 button);

virtual void OnJoyButtonUp(Uint8 which, Uint8 button);

virtual void OnJoyHat(Uint8 which, Uint8 hat, Uint8 value);

virtual void OnJoyBall(Uint8 which, Uint8 ball, Sint16 xrel, Sint16 yrel);

// Window

virtual void OnMinimize();

virtual void OnRestore();

virtu al void OnResize(int w,int h);

virtual void OnExpose();

virtual void OnExit();

virtual void OnUser(Uint8 type, int code, void* data1, void* data2);

};

#endif

 

Pretty hefty class right? Alright, open up CEvent.cpp, and add the following code:

꽤 상당한 수준 맞죠? 좋아, CEvent.cpp을 열고 다음 코드를 추가 :

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

#include "CEvent.h"

CEvent::CEvent() {

}

CEvent::~CEvent() {

//Do nothing

}

void CEvent::OnEvent(SDL_Event* Event) {

switch(Event->type) {

case SDL_ACTIVEEVENT: {

switch(Event->active.state) {

case SDL_APPMOUSEFOCUS: {

if ( Event->active.gain ) OnMouseFocus();

else OnMouseBlur();

break;

}

case SDL_APPINPUTFOCUS: {

if ( Event->active.gain ) OnInputFocus();

else OnInputBlur();

break;

}

case SDL_APPACTIVE: {

if ( Event->active.gain ) OnRestore();

else OnMinimize();

break;

}

}

break;

}

case SDL_KEYDOWN : {

OnKeyDown(Event->key.keysym.sym,

Event->key.keysym.mod,

Event->key.keysym.unicode);

break;

}

case SDL_KEYUP: {

OnKeyUp(Event->key.keysym.sym,

Event->key.keysym.mod,

Event->key.keysym.unicode);

break;

}

case SDL_MOUSEMOTION : {

OnMouseMove(

Event->motion.x,  Event->motion.y,

Event->motion.xrel,  Event->motion.yrel,

(Event->motion.state & SDL_BUTTON(SDL_BUTTON_LEFT) )!=0,

(Event->motion.state & SDL_BUTTON(SDL_BUTTON_RIGHT) )!=0,

(Event->motion.state & SDL_BUTTON(SDL_BUTTON_MIDDLE) )!=0 );

break;

}  

case SDL_MOUSEBUTTONDOWN : {

switch(Event->button.button) {

case SDL_BUTTON_LEFT : {

OnLButtonDown(Event->button.x,Event->button.y);

break;

}

case SDL_BUTTON_RIGHT : {

OnRButtonDown(Event->button.x,Event->button.y);

break;

}

case SDL_BUTTON_MIDDLE : {

OnMButtonDown(Event->button.x,Event->button.y);

break;

}

}

break;

}

case SDL_MOUSEBUTTONUP: {

switch(Event->button.button) {

case SDL_BUTTON_LEFT: {

OnLButtonUp(Event->button.x,Event->button.y);

break;

}

case SDL_BUTTON_RIGHT: {

OnRButtonUp(Event->button.x,Event->button.y);

break;

}

case SDL_BUTTON_MIDDLE: {

OnMButtonUp(Event->button.x,Event->button.y);

break;

}

}

break;

}

case SDL_JOYAXISMOTION: {

OnJoyAxis(Event->jaxis.which,Event->jaxis.axis,Event->jaxis.value);

break;

}

case SDL_JOYBALLMOTION: {

OnJoyBall(Event->jball.which,Event->jball.ball,Event->jball.xrel,Event->jball.yrel);

break;

}

case SDL_JOYHATMOTION: {

OnJoyHat(Event->jhat.which,Event->jhat.hat,Event->jhat.value);

break;

}

case SDL_JOYBUTTONDOWN: {

OnJoyButtonDown(Event->jbutton.which,Event->jbutton.button);

break;

}

case SDL_JOYBUTTONUP: {

OnJoyButtonUp(Event->jbutton.which,Event->jbutton.button);

break;

}

case SDL_QUIT: {

OnExit();

break;

}

case SDL_SYSWMEVENT: {

//Ignore

break;

}

case SDL_VIDEORESIZE: {

OnResize(Event->resize.w,Event->resize.h);

break;

}

case SDL_VIDEOEXPOSE: {

OnExpose();

break;

}

default: {

OnUser(Event->user.type,Event->user.code,Event->user.data1,Event->user.data2);

break;

}

}

}

void CEvent::OnInputFocus() {

//Pure virtual, do nothing

}

void CEvent::OnInputBlur() {

//Pure virtual, do nothing

}

void CEvent::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {

//Pure virtual, do nothing

}

void CEvent::OnKeyUp(SDLKey sym, SDLMod mod, Uint16 unicode) {

//Pure virtual, do nothing

}

void CEvent::OnMouseFocus() {

//Pure virtual, do nothing

}

void CEvent::OnMouseBlur() {

//Pure virtual, do nothing

}

void CEvent::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle) {

//Pure virtual, do nothing

}

void CEvent::OnMouseWheel(bool Up, bool Down) {

//Pure virtual, do nothing

}

void CEvent::OnLButtonDown(int mX, int mY) {

//Pure virtual, do nothing

}

void CEvent::OnLButtonUp(int mX, int mY) {

//Pure virtual, do nothing

}

void CEvent::OnRButtonDown(int mX, int mY) {

//Pure virtual, do nothing

}

void CEvent::OnRButtonUp(int mX, int mY) {

//Pure virtual, do nothing

}

void CEvent::OnMButtonDown(int mX, int mY) {

//Pure virtual, do nothing

}

void CEvent::OnMButtonUp(int mX, int mY) {

//Pure virtual, do nothing

}

void CEvent::OnJoyAxis(Uint8 which,Uint8 axis,Sint16 value) {

//Pure virtual, do nothing

}

void CEvent::OnJoyButtonDown(Uint8 which,Uint8 button) {

//Pure virtual, do nothing

}

void CEvent::OnJoyButtonUp(Uint8 which,Uint8 button) {

//Pure virtual, do nothing

}

void CEvent::OnJoyHat(Uint8 which,Uint8 hat,Uint8 value) {

//Pure virtual, do nothing

}

void CEvent::OnJoyBall(Uint8 which,Uint8 ball,Sint16 xrel,Sint16 yrel) {

//Pure virtual, do nothing

}

void CEvent::OnMinimize() {

//Pure virtual, do nothing

}

void CEvent::OnRestore() {

//Pure virtual, do nothing

}

void CEvent::OnResize(int w,int h) {

//Pure virtual, do nothing

}

void CEvent::OnExpose() {

//Pure virtual, do nothing

}

void CEvent::OnExit() {

//Pure virtual, do nothing

}

void CEvent::OnUser(Uint8 type, int code, void* data1, void* data2) {

//Pure virtual, do nothing

}

}

Lots of code, but all the SDL events should be covered.
What we basically are doing is taking an SDL_Event pointer,
and switching through the types, and then calling the appropriate function.
It just looks like a lot since they are quite a bit of events.
코드가 많지만, 모든 SDL 이벤트가 다루어져야 합니다.
우리가 기본적으로 일을 하는 것은 SDL_Event 포인터를 얻어서,
종류를 통해서 바꿔주고, 해당 함수를 호출합니다.
그들은  이벤트가 꽤 있기 때문에 그냥 많은 것 같이 보인다.


Now that this has been all setup,
let's jump over to CApp.h and link the two together:

이 모든 설정을했습니다
이제 함께 CApp.h에 이동하여, 두개를 연결하자 :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#ifndef _CAPP_H_

#define _CAPP_H_

#include <SDL.h>

#include "CEvent.h"

#include "CSurface.h"

 

class CApp : public CEvent {

 

private:

bool Running;

SDL_Surface* Surf_Display;

SDL_Surface* Surf_Test;

public:

CApp();

int OnExecute();

public:

bool OnInit();

void OnEvent(SDL_Event* Event);

void OnLoop();

void OnRender();

void OnCleanup();

};

#endif

Everything should compile just fine.
We have the event class setup, now lets actually link events to our new class structure.
Open up CApp_OnEvent.cpp and edit the following function:

다 잘 컴파일해야합니다.
우리는 이벤트 클래스 설정을 가지고,
지금은 사실 새로운 클래스 구조에 이벤트를 연결하실 수 있습니다.
CApp_OnEvent.cpp
을 열고 다음 함수를 수정 :

1

2

3

4

5

#include "CApp.h"

void CApp::OnEvent(SDL_Event* Event) {

CEvent::OnEvent(Event);

}

We're passing the Event structure to our class now, it will take care of the correct function calls.

Now when we want to check for an event, we override the function.

Since we got rid of checking for the SDL_QUIT event, lets use the function instead.
Open up CApp.h again, and add the following function:

우리는 이제 클래스에 이벤트 구조를 전달하고, 그것은 올바른 함수 호출을 처리합니다.

이제 이벤트를 확인하고자 할 때, 우리는 기능을 재정의합니다.

우리가 SDL_QUIT 이벤트에 대한 확인을 없앴다고 때문에, 대신 기능을 사용하실 수 있습니다.

CApp.h 다시 열고, 다음과 같은 기능을 추가합니다 :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#ifndef _CAPP_H_

#define _CAPP_H_

#include <SDL.h>

#include "CEvent.h"

#include "CSurface.h"

 

class CApp : public CEvent {

private:

bool Running;

SDL_Surface* Surf_Display;

SDL_Surface* Surf_Test;

public:

CApp();

int OnExecute();

public:

bool OnInit();

void OnEvent(SDL_Event* Event);

void OnExit();

void OnLoop();

void OnRender();

void OnCleanup();

};

#endif

 

The OnExit function handles the SDL_QUIT events.

Now that we have the prototype, lets define what it does.

Open up CApp_OnEvent.cpp, and add the following:

OnExit 함수는 SDL_QUIT 이벤트를 처리합니다.

이제 우리는 프로토 타입을 가지고, 그것이 무엇을 정의할 수 있습니다.

CApp_OnEvent.cpp를 열고 다음을 추가 :

1

2

3

4

5

6

7

8

9

#include "CApp.h"

void CApp::OnEvent(SDL_Event* Event) {

CEvent::OnEvent(Event);

}

void CApp::OnExit() {

Running = false;

}

Recompile, and try it out.
You should be able to close the application just like before.

I encourage you to look through some of the various events that we can check.
Later on we'll use some of these events in our games,
but for now try to familiarize yourself with them.

Jump on over to the next tutorial to take a look at creating our first game, Tic-Tac-Toe.

 

다시 컴파일하고, 그것을 (밖으로) 시도하십시오.
당신은 방금 전에 같은 응용 프로그램을 닫을 수 있습니다.

난 당신이 우리가 확인할 수있는 다양한 이벤트의 일부로 볼 것을 권해드립니다.
나중에 우리는 게임에서 이러한 이벤트 중 일부를 사용하지만,
지금은 그들과 함께 익숙해지도록 노력 해야 합니다..

우리의 첫번째 게임, Tic-Tac-Toe 을 만드는 살펴보기위해, 다음 튜토리얼로 넘어갑시다.

+ Recent posts