//entry point for the window int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) { //Bitmap * bmp = new Bitmap("C:\\Users\\Edwin\\Documents\\Visual Studio 2013\\Projects\\Homework\\Debug\\Temp.bmp"); Level * levelOne = new Level("level1.csv"); Level * floor = new Level("floor.csv"); MainRenderer = new Renderer(); MainRenderer->Init(hInstance, 512, 512); //load floor blocks. for (int y = 0; y < floor->GetHeight(); y++) { for (int x = 0; x < floor->GetWidth(); x++) { if (floor->GetBlock(x, y) == 0) { MainRenderer->AddToScene(new Block(Vector3F(x * 30, y * 30, 30), Block::BlockType::Floor)); } } } //load floor wall blocks for level 1. for (int y = 0; y < levelOne->GetHeight(); y++) { for (int x = 0; x < levelOne->GetWidth(); x++) { if (levelOne->GetBlock(x, y) == 0) { MainRenderer->AddToScene(new Block(Vector3F(x * 30, y * 30, 0), Block::BlockType::Wall)); } } } MainRenderer->SetBackBufferColor(Vector4F(0.0f, 0.5f, 1.0f, 1.0f)); //MainRenderer->GetCamera()->SetPosition(Vector3F(-20.0f, -20.0f, -1000.0f)); position = Vector3F(0, 0, 0.0f); MainRenderer->GetCamera()->SetPosition(position); rotation.X = 280.0f; //HACK this will make player view look a little higher thant it is. position.Z -= 10; //HACK place player origin far back enough they can see the level position.Y -= 200; rotation.Z += 160; //get clockspeed LARGE_INTEGER li; UpdateTimerClockFreq(); INT64 lastDraw = GetTimerCurrentTime(); INT64 lastUpdate = GetTimerCurrentTime(); //while checking for window messages, such as quit/exit while (MainRenderer->Update()) { //check if we should update if (((GetTimerCurrentTime() - lastUpdate) / coreFrequency) > (1000 / targetUPS)) { Update(); lastUpdate = GetTimerCurrentTime(); UpdateTimerClockFreq(); } //check if we should draw, lock at 59fps. this will reduce stutter to a degree but 60 fps would be best. if (((GetTimerCurrentTime() - lastDraw) / coreFrequency) > (900 / targetFPS)) { MainRenderer->Draw(); lastDraw = GetTimerCurrentTime(); UpdateTimerClockFreq(); } } //dispose of memory we no longer need. delete MainRenderer; return 0; }
Ray::Ray(const Vector pos, const Vector dir, const Level& level) { // The player's current grid position inside the level. mMapX = pos.GetX(); mMapY = pos.GetY(); // Length of the ray from one X and Y-side to next X and Y-side. const double delta_dist_x = std::sqrt(std::pow(dir.GetY(), 2) / std::pow(dir.GetX(), 2) + 1); const double delta_dist_y = std::sqrt(std::pow(dir.GetX(), 2) / std::pow(dir.GetY(), 2) + 1); int step_x; // +1 if heading north, -1 if heading south. int step_y; // +1 if heading east, -1 if heading west. double side_dist_x; double side_dist_y; if (dir.GetX() < 0) { step_x = -1; side_dist_x = (pos.GetX() - mMapX) * delta_dist_x; } else { step_x = 1; side_dist_x = (mMapX + 1. - pos.GetX()) * delta_dist_x; } if (dir.GetY() < 0) { step_y = -1; side_dist_y = (pos.GetY() - mMapY) * delta_dist_y; } else { step_y = 1; side_dist_y = (mMapY + 1. - pos.GetY()) * delta_dist_y; } for (;;) { if (side_dist_x < side_dist_y) { // Jump one square in X-direction. mMapX += step_x; side_dist_x += delta_dist_x; mVerticalSideHit = false; } else { // Jump one square in Y-direction. mMapY += step_y; side_dist_y += delta_dist_y; mVerticalSideHit = true; } // Check if the ray has hit a wall. if ((mMapX < 0) || (mMapX >= level.GetWidth()) || (mMapY < 0) || (mMapY >= level.GetHeight()) || level.IsBlocking(mMapX, mMapY)) { break; } } if (!mVerticalSideHit) { const double unit_x = mMapX + (1 - step_x) / 2; const double intersect_x = pos.GetY() + ((unit_x - pos.GetX()) / dir.GetX()) * dir.GetY(); mIntersection = { unit_x, intersect_x }; // FIXME: May be to small. mDistance = std::fabs((unit_x - pos.GetX()) / dir.GetX()); } else { const double unit_y = mMapY + (1 - step_y) / 2; const double intersect_y = pos.GetX() + ((unit_y - pos.GetY()) / dir.GetY()) * dir.GetX(); mIntersection = { unit_y, intersect_y }; // FIXME: May be too small. mDistance = std::fabs((unit_y - pos.GetY()) / dir.GetY()); } }