Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ if(EGE_ENABLE_CPP17)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()

file(GLOB EGE_CPP_SRC CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE EGE_CPP_SRC CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
add_library(xege STATIC ${EGE_CPP_SRC})

target_include_directories(xege PUBLIC include)
Expand Down
8 changes: 6 additions & 2 deletions src/ege_head.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#define EGE_WNDCLSNAME_W EGE_L(EGE_WNDCLSNAME)

#include <string>
#include <thread>

#ifdef EGE_GDIPLUS
# if defined(NOMINMAX) && defined(_MSC_VER)
Expand All @@ -98,6 +99,7 @@
#endif

#include "thread_queue.h"
#include "sync/semaphore.h"

#ifndef ERROR_SUCCESS
#define ERROR_SUCCESS 0
Expand Down Expand Up @@ -165,7 +167,8 @@ class egeControlBase; // egeControlBase 前置声明
// 定义ege全局状态对象
struct _graph_setting
{
bool has_init;
Semaphore init_sem;

bool unicode_char_message;

struct _graph
Expand Down Expand Up @@ -209,7 +212,7 @@ struct _graph_setting

thread_queue<EGEMSG>*msgkey_queue, *msgmouse_queue;

HANDLE threadui_handle;
std::thread threadui;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/* 鼠标状态记录 */
Point mouse_pos;
Expand Down Expand Up @@ -251,6 +254,7 @@ struct _graph_setting

public:
_graph_setting();
~_graph_setting();
};

template <typename T> struct count_ptr
Expand Down
6 changes: 3 additions & 3 deletions src/egegapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool is_run()

bool isinitialized()
{
return graph_setting.has_init;
return graph_setting.init_sem.acquirable();
}

int showmouse(int bShow)
Expand Down Expand Up @@ -675,7 +675,7 @@ color_t getbkcolor(PCIMAGE pimg)
}
} else {
_graph_setting* pg = &graph_setting;
if (!pg->has_init) {
if (!pg->init_sem.acquirable()) {
return pg->window_initial_color;
}
}
Expand Down Expand Up @@ -714,7 +714,7 @@ void setbkcolor_f(color_t color, PIMAGE pimg)
}
} else {
_graph_setting* pg = &graph_setting;
if (!pg->has_init) {
if (!pg->init_sem.acquirable()) {
pg->window_initial_color = color;
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ unsigned long getlogodatasize();

DWORD WINAPI messageloopthread(LPVOID lpParameter);

_graph_setting::_graph_setting()
_graph_setting::_graph_setting() : init_sem{0}
{
window_caption = EGE_TITLE_W;
window_initial_color = IMAGE::initial_bk_color;
}

_graph_setting::~_graph_setting()
{
if (threadui.joinable()) {
threadui.join();
}
}

/*private function*/
static void ui_msg_process(EGEMSG& qmsg)
{
Expand Down Expand Up @@ -799,7 +806,7 @@ void logoscene()

inline void init_img_page(struct _graph_setting* pg)
{
if (!pg->has_init) {
if (!pg->init_sem.acquirable()) {
#ifdef EGE_GDIPLUS
gdiplusinit();
#endif
Expand Down Expand Up @@ -867,7 +874,7 @@ void initgraph(int* gdriver, int* gmode, const char* path)
dll::loadDllsIfNot();

// 已创建则转为改变窗口大小
if (pg->has_init) {
if (pg->init_sem.acquirable()) {
int width = (short)(*gmode & 0xFFFF);
int height = (short)((unsigned int)(*gmode) >> 16);
resizewindow(width, height);
Expand All @@ -889,14 +896,9 @@ void initgraph(int* gdriver, int* gmode, const char* path)
// 注册窗口类,设置默认消息处理函数, 此处创建 Unicode 窗口
register_classW(pg, pg->instance);

// SECURITY_ATTRIBUTES sa = {0};
DWORD pid;
pg->threadui_handle = CreateThread(NULL, 0, messageloopthread, pg, CREATE_SUSPENDED, &pid);
ResumeThread(pg->threadui_handle);

while (!pg->has_init) {
::Sleep(1);
}
pg->threadui = std::thread{messageloopthread, pg};
pg->init_sem.acquire();
pg->init_sem.add_permit();

UpdateWindow(pg->hwnd);

Expand Down Expand Up @@ -977,7 +979,7 @@ DWORD WINAPI messageloopthread(LPVOID lpParameter)
pg->skip_timer_mark = false;
SetTimer(pg->hwnd, RENDER_TIMER_ID, 50, NULL);

pg->has_init = true;
pg->init_sem.add_permit();

while (!pg->exit_window) {
if (GetMessageW(&msg, NULL, 0, 0)) {
Expand Down
30 changes: 30 additions & 0 deletions src/sync/semaphore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "semaphore.h"

namespace ege
{

bool Semaphore::acquirable()
{
std::lock_guard lock{mut};
return counter > 0;
}

void Semaphore::acquire()
{
std::unique_lock lock{mut};
cv.wait(lock, [this] { return counter > 0; });
counter--;
}

int Semaphore::add_permit()
{
int old;
{
std::lock_guard lock{mut};
old = counter++;
}
cv.notify_one();
return old;
}

} // namespace ege
25 changes: 25 additions & 0 deletions src/sync/semaphore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <condition_variable>
#include <mutex>

namespace ege
{

class Semaphore
{
private:
int counter;
std::condition_variable cv;
std::mutex mut;

public:
Semaphore(int c) : counter{c} {}

bool acquirable();
void acquire();

int add_permit();
};

} // namespace ege
4 changes: 2 additions & 2 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void setcaption(const char* caption)
void setcaption(const wchar_t* caption)
{
struct _graph_setting* pg = &graph_setting;
if (pg->has_init) {
if (pg->init_sem.acquirable()) {
::SetWindowTextW(getHWnd(), caption);
::UpdateWindow(getHWnd()); // for vc6
}
Expand All @@ -43,7 +43,7 @@ void seticon(int icon_id)
}
if (hIcon) {
pg->window_hicon = hIcon;
if (pg->has_init) {
if (pg->init_sem.acquirable()) {
#ifdef _WIN64
::SetClassLongPtrW(getHWnd(), GCLP_HICON, (LONG_PTR)hIcon);
#else
Expand Down
Loading