Ejemplo n.º 1
0
void display (void){
    
    //SDL_PollEvent( &event );
    
    //if( event.type == SDL_QUIT ) { 
    //    displayEnd(); 
    //}
                    
    
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    updateTexture();
    //setupTexture();
    //SDL_UpdateWindowSurface( window );
    SDL_GL_SwapWindow(window);
    fpsthink();
    //SDL_Delay(50);
}
Ejemplo n.º 2
0
int main(int, char**) {


	//initialise SDL system and the video subsystem
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		QuitWithError("SDL_Init Error: ");

	}

	//Create a Window
	SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
	if (win == nullptr) {
		QuitWithError("SDL_CreateWindow Error: ");

	}

	//SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN);

	Mix_Music *music = NULL;
	Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1;
	std::string basepath(SDL_GetBasePath());
	music = Mix_LoadMUS((basepath + "background.wav").c_str());
	Mix_PlayMusic(music, -1);

	Render* renderer = new Render(win);

	//Main loop flag
	bool quit = false;
	b2Vec2 Gravity(0.f, 0.0098f);
	b2World World(Gravity);

	string bgPath = basepath + "background22.bmp";
	SDL_Surface* back = SDL_LoadBMP(bgPath.c_str());
	SDL_Rect* backGroundRect = renderer->AddSurfaceToRenderer(back, 0, -600, 1.0f);

	Button button = Button(-200, -50, World, renderer);
	Door door = Door(600, -100, renderer);

	Level level = Level(World, renderer);
	Player player = Player(100, 500, World, renderer);

	vector<Cannon*> cannons;
	cannons.push_back(new Cannon(170, 350, World, renderer, 1));
	cannons.push_back(new Cannon(1010, 50, World, renderer, 2));
	cannons.push_back(new Cannon(170, 450, World, renderer, 1));
	cannons.push_back(new Cannon(1010, 150, World, renderer, 2));
	cannons.push_back(new Cannon(170, 550, World, renderer, 1));
	cannons.push_back(new Cannon(1010, 250, World, renderer, 2));

	MenuScene* menu = new MenuScene(1200, 100, renderer);
	SDL_Event e;

	//thread t1(&Process::run, Process((*(game)))); //Passing references
	//t1.detach(); //detaches from SDL mainline

	float prevTicks = SDL_GetTicks();
	float currentTicks = SDL_GetTicks();
	float FPS = 0;

	int fpsTimer = 0;

	SDL_Init(0);

	//game loop
	while (!quit) {

		World.Step(1 / 60.f, 6, 2);
		while (SDL_PollEvent(&e) != 0) {
			if (inputHandler.CheckInput(SDLK_ESCAPE, e)) {
				quit = true;
			}
		}
		if (menu->playBool == false && menu->quitBool == false) {
			renderer->DrawMenuScene();
			menu->Update(renderer);
		}
		
		if (menu->playBool == true)
		{
			//PLAY GAME STATE
			int dir = player.Move(inputHandler, e);

			for (int i = 0; i < cannons.size(); i++)
			{
				cannons.at(i)->Update();
			}

			SDL_Rect rec(player.spriteRect);
			rec.y = player.GetY();

			for (int j = 0; j < cannons.size(); j++)
			{
				for (int i = 0; i < cannons.at(j)->fireballs.size(); i++)
				{
					if (cannons.at(j)->fireballs.at(i)->CheckCollision(&rec) == true)
					{
						std::cout << "Collision Detected!" << std::endl;
						player.Respawn();
						button.setOnce(false);
						button.buttonBody->SetTransform(b2Vec2(880 / SCALE, 39 / SCALE), 0);
						door.spriteRect->x = -1000;
						door.spriteRect->y = -1000;
						player.prevPosX.clear();
						player.prevPosY.clear();
						player.count = 0;
					}
				}
			}

			button.Update();
			
			if (button.CheckCollision(&rec) == true)
			{
				std::cout << "Collision Detected!" << std::endl;
				button.collision = true;
				button.spriteRect->x = -2000;
				button.spriteRect->y = -2000;
				button.buttonBody->SetTransform(b2Vec2(-2000, -2000), 0);
				//door.Draw(renderer);
			}
			if (door.CheckCollision(&rec) == true)
			{
				button.buttonBody->SetTransform(b2Vec2(880 / SCALE, 39/ SCALE), 0);
				std::cout << "Collision Detected!" << std::endl;
				player.Respawn();
				button.setOnce(false);
				door.spriteRect->x = -1000;
				door.spriteRect->y = -1000;
				player.prevPosX.clear();
				player.prevPosY.clear();
				player.count = 0;
				player.Respawn();
				menu->playBool = false;
				menu->quitBool = false;
				menu->backGroundRect->x = 0;
				menu->current = 0;
				button.collision = false;
			}
			if (button.collision == false)
			{
				door.DrawCage(renderer);
			}
			if (button.collision == true)
			{
				door.DrawNoCage(renderer);
			}

			int ticks = SDL_GetTicks();
			int seconds = ticks / 50;
			int sprite = seconds % 8;
			renderer->Update(player.srcRect, player.dstRect, *player.LeftTexture, *player.RightTexture, *player.StandTexture, sprite, dir, player.moving, player.GetY());
			player.dstRect.w = player.spriteRect.w;
			player.dstRect.h = player.spriteRect.h;
			player.dstRect.x = player.spriteRect.x;
			player.dstRect.y = player.spriteRect.y + 5;
		}

		if (menu->quitBool == true)
		{
			quit = true;
		}

		fpsthink();

		if (fpsTimer == 60)
		{
			printf("%f\n", framespersecond);
			fpsTimer = 0;
		}

		fpsTimer++;
	}
	SDL_DestroyRenderer(renderer->ren);
	SDL_DestroyWindow(win);
	SDL_DestroyMutex(mutex);

	SDL_Quit();
	return 0;
}