QList<QObject*> GetDataFilters (const QVariant& data, IEntityManager* manager) { const auto& e = MakeEntity (data, QString (), {}, "x-leechcraft/data-filter-request"); const auto& handlers = manager->GetPossibleHandlers (e); QList<QObject*> result; std::copy_if (handlers.begin (), handlers.end (), std::back_inserter (result), [] (QObject *obj) { return qobject_cast<IDataFilter*> (obj); }); return result; }
Entity MakeNotification (const QString& header, const QString& text, Priority priority) { Entity result = MakeEntity (header, QString (), AutoAccept | OnlyHandle, "x-leechcraft/notification"); result.Additional_ ["Text"] = text; result.Additional_ ["Priority"] = priority; return result; }
StdDataFilterMenuCreator::StdDataFilterMenuCreator (const QVariant& dataVar, IEntityManager *em, QMenu *menu) : QObject (menu) , EntityMgr_ (em) { auto entity = MakeEntity (dataVar, QString (), FromUserInitiated | OnlyHandle, "x-leechcraft/data-filter-request"); for (auto plugin : em->GetPossibleHandlers (entity)) { auto ii = qobject_cast<IInfo*> (plugin); auto idf = qobject_cast<IDataFilter*> (plugin); if (!idf) continue; const auto& vars = idf->GetFilterVariants (dataVar); if (vars.isEmpty ()) continue; const auto actor = [this, entity, plugin] (const IDataFilter::FilterVariant& var) mutable { entity.Additional_ ["DataFilter"] = var.ID_; EntityMgr_->HandleEntity (entity, plugin); ChosenPlugin_ = qobject_cast<IInfo*> (plugin)->GetUniqueID (); ChosenVariant_ = var.ID_; }; if (vars.size () == 1) AddDatafilterMenuItem (vars.value (0), menu, actor); else { auto searchMenu = menu->addMenu (ii->GetIcon (), idf->GetFilterVerb ()); for (const auto& var : vars) AddDatafilterMenuItem (var, searchMenu, actor); } } }
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX window_class = {}; window_class.cbSize = sizeof(window_class); window_class.style = CS_HREDRAW | CS_VREDRAW; window_class.lpfnWndProc = WindowCallback; window_class.hInstance = hInstance; window_class.hCursor = LoadCursor(NULL, IDC_ARROW); window_class.lpszClassName = "WCPetroCanada"; RegisterClassEx(&window_class); HWND window = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "WCPetroCanada", "Game :)", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, NULL, NULL, hInstance, NULL); ShowWindow(window, nCmdShow); UpdateWindow(window); RECT client_rect = {}; GetClientRect(window, &client_rect); Framebuffer backbuffer = {}; backbuffer.width = client_rect.right; backbuffer.height = client_rect.bottom; backbuffer.pixels = (uint32_t *)VirtualAlloc(NULL, backbuffer.width * backbuffer.height * sizeof(uint32_t ), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); HDC window_context = GetDC(window); BITMAPINFO bitmap_info = {}; bitmap_info.bmiHeader.biSize = sizeof(bitmap_info); bitmap_info.bmiHeader.biWidth = backbuffer.width; bitmap_info.bmiHeader.biHeight = backbuffer.height; bitmap_info.bmiHeader.biPlanes = 1; bitmap_info.bmiHeader.biBitCount = 32; bitmap_info.bmiHeader.biCompression = BI_RGB; LoadedBitmap player_head = LoadBitmap("petro.bmp"); MSG message; bool open = true; Vector2 velocity = V2(0, 0); Vector2 position = V2(200, 200); float timestep = 1.0f; //TODO proper timestep EntityArray entities = {}; Entity *player_entity = MakeEntity(&entities, EntityType_Player, V2(200, 200), player_head.width, player_head.height, V2(10, 10)); MakeEntity(&entities, EntityType_Box, V2(50, 50), 100, 100); bool w_down = false; bool a_down = false; bool s_down = false; bool d_down = false; bool q_down = false; while(open) { while(PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) { if(message.message == WM_QUIT) { open = false; } else if(message.message == WM_KEYDOWN) { if(message.wParam == W_KEY_CODE) { w_down = true; } else if(message.wParam == S_KEY_CODE) { s_down = true; } else if(message.wParam == D_KEY_CODE) { d_down = true; } else if(message.wParam == A_KEY_CODE) { a_down = true; } else if(message.wParam == Q_KEY_CODE) { q_down = true; } } else if(message.message == WM_KEYUP) { if(message.wParam == W_KEY_CODE) { w_down = false; } else if(message.wParam == S_KEY_CODE) { s_down = false; } else if(message.wParam == D_KEY_CODE) { d_down = false; } else if(message.wParam == A_KEY_CODE) { a_down = false; } else if(message.wParam == Q_KEY_CODE) { q_down = false; } } TranslateMessage(&message); DispatchMessage(&message); } DrawRectangle(0, 0, backbuffer.width, backbuffer.height, backbuffer, V4(0.0f, 0.0f, 0.0f, 1.0f)); DrawRectangle(100, 100, 100, 100, backbuffer, V4(position.x / (float)backbuffer.width, position.y / (float)backbuffer.height, 0.0f, 1.0f)); DrawRectangle(100, 100, 150, 150, backbuffer, V4(0.0f, 1.0f, 0.0f, 0.25f)); for(int i = 0; i < entities.count; i++) { Entity *entity = entities.entities + i; if(entity->type != EntityType_Invalid) { Vector2 acceleration = V2(0, 0); if(entity == player_entity) { float multiplier = 1.0f; if(w_down) { acceleration = acceleration + V2(0, 1); } if(s_down) { acceleration = acceleration + V2(0, -1); } if(d_down) { acceleration = acceleration + V2(1, 0); } if(a_down) { acceleration = acceleration + V2(-1, 0); } if(q_down) { multiplier = 5.0f; } if(Length(acceleration) > 0) { acceleration = Normalize(acceleration) * multiplier; } } acceleration = acceleration - (entity->velocity * 0.05f); Vector2 new_velocity = (acceleration * timestep) + entity->velocity; Vector2 new_position = (acceleration * 0.5f * timestep * timestep) + (new_velocity * timestep) + GetCenter(entity->bounds); Rect2 collision_box = RectPosSize(new_position.x, new_position.y, GetWidth(entity->bounds), GetHeight(entity->bounds)); bool intersects = false; for(int j = 0; j < entities.count; j++) { if(j != i) { Entity *other_entity = entities.entities + j; intersects = intersects || Intersect(other_entity->bounds, collision_box); } } if(!intersects) { entity->velocity = new_velocity; entity->bounds = RectPosSize(new_position.x, new_position.y, GetWidth(entity->bounds), GetHeight(entity->bounds)); } else { entity->velocity = V2(0, 0); } switch(entity->type) { case EntityType_Box: { DrawRectangle(entity->bounds.min.x, entity->bounds.min.y, GetWidth(entity->bounds), GetHeight(entity->bounds), backbuffer, V4(1.0f, 0.0f, 0.0f, 1.0f)); } break; case EntityType_Player: { float angle = GetAngle(entity->velocity); Direction direction = AngleToDirection(angle); Vector4 color = V4(1.0f, 1.0f, 1.0f, 0.0f); if(direction == Direction_Up) { color = V4(1.0f, 0.0f, 0.0f, 1.0f); } else if(direction == Direction_Down) { color = V4(1.0f, 1.0f, 0.0f, 1.0f); } else if(direction == Direction_Left) { color = V4(0.0f, 1.0f, 1.0f, 1.0f); } else if(direction == Direction_Right) { color = V4(0.0f, 1.0f, 0.0f, 1.0f); } char output[255]; sprintf(output, "%f\n", angle); OutputDebugStringA(output); DrawRectangle(300, 300, 10, 10, backbuffer, color); DrawRectangle(entity->bounds.min.x, entity->bounds.min.y, GetWidth(entity->bounds), GetHeight(entity->bounds), backbuffer, intersects ? V4(0.5f, 0.0f, 0.0f, 0.5f) : V4(0.5f, 0.5f, 0.5f, 0.5f)); DrawBitmap(entity->bounds.min.x, entity->bounds.min.y, GetWidth(entity->bounds), GetHeight(entity->bounds), backbuffer, player_head); } break; } } } StretchDIBits(window_context, 0, 0, backbuffer.width, backbuffer.height, 0, 0, backbuffer.width, backbuffer.height, backbuffer.pixels, &bitmap_info, DIB_RGB_COLORS, SRCCOPY); } return 0; }