[wxWidgets] Timer 타이머

C, C++ 2026. 5. 10. 19:41 |
반응형

타이머를 사용해 보자.

 

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
	bool OnInit() override;
};

wxIMPLEMENT_APP(MyApp);

class MyFrame : public wxFrame
{
public:
	MyFrame();

private:
	wxTimer m_timer; // 타이머 객체 추가

	void OnHello(wxCommandEvent& event);
	void OnExit(wxCommandEvent& event);
	void OnAbout(wxCommandEvent& event);
	void OnTimer(wxTimerEvent& event); // 타이머 이벤트 핸들러 추가
};

enum
{
	ID_Hello = 1
};

bool MyApp::OnInit()
{
	MyFrame* frame = new MyFrame();
	frame->Show(true);
	return true;
}

MyFrame::MyFrame() : wxFrame(nullptr, wxID_ANY, "Hello World")
{
	wxMenu* menuFile = new wxMenu;
	menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");
	menuFile->AppendSeparator();
	menuFile->Append(wxID_EXIT);

	wxMenu* menuHelp = new wxMenu;
	menuHelp->Append(wxID_ABOUT);

	wxMenuBar* menuBar = new wxMenuBar;
	menuBar->Append(menuFile, "&File");
	menuBar->Append(menuHelp, "&Help");

	SetMenuBar(menuBar);

	CreateStatusBar();
	SetStatusText("Welcome to wxWidgets!");

	// 타이머 초기화 및 시작 방법 1: 타이머 객체에 직접 이벤트 핸들러 바인딩
	m_timer.Bind(wxEVT_TIMER, &MyFrame::OnTimer, this, m_timer.GetId()); // 타이머 이벤트 핸들러 바인딩	
	m_timer.Start(1000); // 1초마다 타이머 이벤트 발생

	// 타이머 초기화 및 시작 방법 2: 프레임에서 타이머 이벤트 핸들러 바인딩
	//m_timer.SetOwner(this); // 타이머의 소유자를 현재 프레임으로 설정
	//Bind(wxEVT_TIMER, &MyFrame::OnTimer, this, m_timer.GetId()); // 현재 프레임에서 타이머 이벤트 핸들러 바인딩
	//m_timer.Start(1000); // 1초마다 타이머 이벤트 발생

	wxPanel* panel = new wxPanel(this, wxID_ANY);
	wxButton* myButton = new wxButton(panel, wxID_ANY, "Press Me", wxPoint(30, 30), wxSize(100, 30));
	myButton->Bind(wxEVT_BUTTON, &MyFrame::OnHello, this);
	//Bind(wxEVT_BUTTON, &MyFrame::OnHello, this, myButton->GetId());

	Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
	Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
	Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}

void MyFrame::OnExit(wxCommandEvent& event)
{
	Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& event)
{
	wxMessageBox("This is a wxWidgets Hello World example", "About Hello World", wxOK | wxICON_INFORMATION);
}

void MyFrame::OnHello(wxCommandEvent& event)
{
	wxLogMessage("Hello world from wxWidgets!");
}

void MyFrame::OnTimer(wxTimerEvent& event) // 타이머 이벤트 핸들러 구현
{
	static int count = 0;
	count++;
	SetStatusText(wxString::Format("Timer tick: %d", count));
}

 

상태표시줄에 타이머 동작이 표시된다.

 

복수의 타이머도 동일한 방법으로 생성한다.

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
	bool OnInit() override;
};

wxIMPLEMENT_APP(MyApp);

class MyFrame : public wxFrame
{
public:
	MyFrame();

private:
	wxTimer m_timer; // 타이머 객체 추가
	wxTimer m_timer2; // 타이머 객체 추가

	void OnHello(wxCommandEvent& event);
	void OnExit(wxCommandEvent& event);
	void OnAbout(wxCommandEvent& event);
	void OnTimer(wxTimerEvent& event); // 타이머 이벤트 핸들러 추가
	void OnTimer2(wxTimerEvent& event); // 타이머 이벤트 핸들러 추가
};

enum
{
	ID_Hello = 1
};

bool MyApp::OnInit()
{
	MyFrame* frame = new MyFrame();
	frame->Show(true);
	return true;
}

MyFrame::MyFrame() : wxFrame(nullptr, wxID_ANY, "Hello World")
{
	wxMenu* menuFile = new wxMenu;
	menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item");
	menuFile->AppendSeparator();
	menuFile->Append(wxID_EXIT);

	wxMenu* menuHelp = new wxMenu;
	menuHelp->Append(wxID_ABOUT);

	wxMenuBar* menuBar = new wxMenuBar;
	menuBar->Append(menuFile, "&File");
	menuBar->Append(menuHelp, "&Help");

	SetMenuBar(menuBar);

	CreateStatusBar();
	SetStatusText("Welcome to wxWidgets!");

	// 타이머 초기화 및 시작 방법 1: 타이머 객체에 직접 이벤트 핸들러 바인딩
	m_timer.Bind(wxEVT_TIMER, &MyFrame::OnTimer, this, m_timer.GetId()); // 타이머 이벤트 핸들러 바인딩	
	m_timer.Start(1000); // 1초마다 타이머 이벤트 발생

	m_timer2.Bind(wxEVT_TIMER, &MyFrame::OnTimer2, this, m_timer2.GetId()); // 타이머2 이벤트 핸들러 바인딩
	m_timer2.Start(1000); // 1초마다 타이머2 이벤트 발생

	// 타이머 초기화 및 시작 방법 2: 프레임에서 타이머 이벤트 핸들러 바인딩
	//m_timer.SetOwner(this); // 타이머의 소유자를 현재 프레임으로 설정
	//Bind(wxEVT_TIMER, &MyFrame::OnTimer, this, m_timer.GetId()); // 현재 프레임에서 타이머 이벤트 핸들러 바인딩
	//m_timer.Start(1000); // 1초마다 타이머 이벤트 발생

	//m_timer2.SetOwner(this); // 타이머2의 소유자를 현재 프레임으로 설정
	//Bind(wxEVT_TIMER, &MyFrame::OnTimer2, this, m_timer2.GetId()); // 현재 프레임에서 타이머2 이벤트 핸들러 바인딩
	//m_timer2.Start(1000); // 1초마다 타이머2 이벤트 발생

	wxPanel* panel = new wxPanel(this, wxID_ANY);
	wxButton* myButton = new wxButton(panel, wxID_ANY, "Press Me", wxPoint(30, 30), wxSize(100, 30));
	myButton->Bind(wxEVT_BUTTON, &MyFrame::OnHello, this);
	//Bind(wxEVT_BUTTON, &MyFrame::OnHello, this, myButton->GetId());

	Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
	Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
	Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}

void MyFrame::OnExit(wxCommandEvent& event)
{
	Close(true);
}

void MyFrame::OnAbout(wxCommandEvent& event)
{
	wxMessageBox("This is a wxWidgets Hello World example", "About Hello World", wxOK | wxICON_INFORMATION);
}

void MyFrame::OnHello(wxCommandEvent& event)
{
	wxLogMessage("Hello world from wxWidgets!");
}

void MyFrame::OnTimer(wxTimerEvent& event) // 타이머 이벤트 핸들러 구현
{
	static int count = 0;
	count++;
	SetStatusText(wxString::Format("Timer tick: %d", count));
}

void MyFrame::OnTimer2(wxTimerEvent& event) // 타이머 이벤트 핸들러 구현
{
	static int count = 0;
	count++;
	SetTitle(wxString::Format("Timer2 tick: %d", count));
}

 

 

반응형
Posted by J-sean
:
반응형

코드로 캐릭터를 생성해 보자.

 

스프라이트를 준비하고 스크립트(Player.cs)를 추가한다.

 

타이머를 자식노드로 추가한다. Autostart를 체크하고 Wait Time은 5초로 설정한다.

 

timeout 시그널을 스프라이트에 연결한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using Godot;
 
public partial class Player : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
 
    public void OnTimerTimeout()
    {
        QueueFree();
    }
}
 

 

 

타임아웃 시그널이 발생하면 삭제 큐에 노드를 추가하는(QueuFree) Player.cs 스크립트를 작성한다.

 

 

res:// 에 스크립트(Controls.cs)를 추가한다.

 

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
using Godot;
 
public partial class Control : Node
{
    public PackedScene Scene;
    public Timer timer;
 
    public override void _Ready()
    {
        // C# has no preload, so you have to always use ResourceLoader.Load<PackedScene>().
        Scene = ResourceLoader.Load<PackedScene>("res://Player.tscn");
        
        timer = new Timer();
        timer.Connect("timeout", Callable.From(OnTimeOut));
        timer.WaitTime = 1.0;
        AddChild(timer);
        timer.Start();
    }
    
    public override void _Process(double delta)
    {        
    }
 
    public void OnTimeOut()
    {
        Sprite2D Sprite = Scene.Instantiate<Sprite2D>();
        Sprite.Position = new Vector2(GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.X),
            GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.Y));
        AddChild(Sprite);
    }
}
 

 

 

Control.cs를 작성한다. _Ready()에서 타이머를 코드로 생성하고 타임아웃 시그널이 발생하면 씬을 생성한다.

 

Project Settings - Autoload 에 Controls.cs를 추가한다.

 

게임을 실행하면 캐릭터가 생성되고 잠시 후 사라진다.

※ 참고

Nodes and scenes

Callable

 

반응형
Posted by J-sean
:
반응형

타이머가 보내는 시그널을 받아보자.

 

Sprite2D 노드를 생성하고 Texture를 연결한다.

 

Sprite2D 노드의 자식 노드로 Timer 노드를 생성하고 Autostart 속성을 활성화 한다.

 

Sprite2D 노드에 스크립트를 생성한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using Godot;
 
public partial class player : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        Timer timer = GetNode<Timer>("Timer");
        timer.Timeout += OnTimeout;
    }
 
    private void OnTimeout()
    {
        GD.Print("Timeout!!");
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
}
 

 

 

Timeout 시그널을 받는 스크립트를 작성한다.

 

 

Sprite2D 노드에 스크립트(player.cs)가 생성되었다.

 

씬을 저장하고 게임을 실행하면 Output 창에 Timeout!! 메세지가 반복해서 출력된다.

 

※ 참고

Using signals

C# signals

 

반응형
Posted by J-sean
:
반응형

간단하고 저렴한 딜레이 타이머 C005를 사용해 보자.

 

가로 세로 약 1cm 정도의 작고 저렴한 딜레이 타이머.

 

P1 패드를 쇼트 시키면 8배, P2 패드를 쇼트 시키면 64배, 둘 다 쇼트 시키면 시간을 512배 딜레이 시킬 수 있다.

 

위와 같이 회로를 구성하면 타이밍 저항값에 따라 LED를 정해진 시간만큼 켤 수 있다.

좀 더 안정적인 스위칭이 필요하다면 스위치에 pull-up 저항을 구성한다.

 

P1, P2 패드가 모두 연결 되지 않은 경우 타이밍 저항과 전압에 따른 지연 시간.

 

 

반응형
Posted by J-sean
:
반응형

Timer와 TimerTask를 이용해 정해진 시간에 반복적인 작업을 할 수 있다.


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
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
    Timer timer;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = findViewById(R.id.textView);
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd EEE HH:mm:ss");
 
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                // Only the original thread that created a view hierarchy can touch its views.
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String currentTime = simpleDateFormat.format(new Date());
                        textView.setText(currentTime);
                    }
                });
            }
        };
 
        timer = new Timer();
        timer.schedule(timerTask, 01000);
    }
 
    @Override
    protected void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }
}




반응형
Posted by J-sean
: