Add pause menu and main menu

This commit is contained in:
Furkan Mudanyali 2021-12-13 01:12:00 +03:00
parent 4832492f1e
commit fba115173c
14 changed files with 222 additions and 22 deletions

BIN
assets/continue.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
assets/continuesel.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
assets/exit.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
assets/exitsel.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
assets/menu.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 KiB

BIN
assets/pause.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 KiB

BIN
assets/start.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
assets/startsel.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@ -31,7 +31,7 @@ public class Main {
}
Render.init();
Screen.init();
scenes.push(new Game());
scenes.push(new MainMenu());
while(!scenes.empty() && !exit){
Time.Tick();

View File

@ -44,7 +44,7 @@ public class Render {
WIDTH, HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
renderer =
SDL_CreateRenderer(Render.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

View File

@ -25,21 +25,19 @@ import static org.libsdl.api.event.SdlEvents.*;
import static com.fmudanyali.Render.*;
import static org.libsdl.api.keycode.SDL_Keycode.*;
import static org.libsdl.api.render.SdlRender.*;
import static org.libsdl.api.error.SdlError.SDL_GetError;
//import static org.libsdl.api.error.SdlError.SDL_GetError;
public class Game extends Scene {
public static boolean exit = false;
public static boolean escPressed = false;
public int kek = 0;
public Player player = new Player();
int kek = 0;
private Player player = new Player();
public Game() throws Exception{
Screen.makeBackground("scene1/tile.bmp");
}
@Override
public void loop(){
public void loop() throws Exception{
while(SDL_PollEvent(Main.e) != 0){
switch(Main.e.type){
case SDL_QUIT:
@ -67,13 +65,11 @@ public class Game extends Scene {
player.movement();
Screen.scroll();
/*
++kek;
if(kek == 60){
System.out.println(SDL_GetError());
System.out.println(escPressed);
kek = 0;
}
*/
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, Screen.wallpaper, null, null);

View File

@ -17,6 +17,128 @@
package com.fmudanyali.scenes;
import com.fmudanyali.Main;
import com.fmudanyali.Screen;
import com.fmudanyali.FileLoader;
import org.libsdl.api.rect.SDL_Rect;
import org.libsdl.api.render.*;
import org.libsdl.api.surface.SDL_Surface;
import static com.fmudanyali.Render.*;
import static org.libsdl.api.render.SdlRender.*;
import static org.libsdl.api.surface.SdlSurface.*;
import static org.libsdl.api.event.SdlEvents.*;
import static org.libsdl.api.keycode.SDL_Keycode.*;
public class MainMenu extends Scene {
public void loop(){};
private SDL_Surface tempSurface;
private SDL_Texture startTexture, exitTexture, background;
private SDL_Rect startPos = new SDL_Rect();
private SDL_Rect exitPos = new SDL_Rect();
private SDL_Rect buttonSize = new SDL_Rect();
private boolean enterPressed = false;
private boolean wPressed = false;
private boolean sPressed = false;
private int selection = 0;
public MainMenu() throws Exception{
buttonSize.x = buttonSize.y = 0;
buttonSize.w = startPos.w = exitPos.w = 150;
buttonSize.h = startPos.h = exitPos.h = 100;
startPos.x = (Screen.WIDTH - Screen.CANV_W)/2 + 10;
startPos.y = (Screen.HEIGHT - Screen.CANV_H)/2 + 10;
exitPos.x = (Screen.WIDTH - Screen.CANV_W)/2 + 10;
exitPos.y = (Screen.HEIGHT - Screen.CANV_H)/2 + 120;
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("menu.bmp"));
background = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("startsel.bmp"));
startTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("exit.bmp"));
exitTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
}
@Override
public void loop() throws Exception{
while(SDL_PollEvent(Main.e) != 0){
switch(Main.e.type){
case SDL_QUIT:
Main.exit = true;
break;
case SDL_KEYDOWN:
switch(Main.e.key.keysym.sym){
case SDLK_RETURN:
if(!enterPressed){
confirm();
enterPressed = true;
}
break;
case SDLK_w:
if(!wPressed){
selection = Math.min(selection - 1, 1);
wPressed = true;
updateButtons();
}
break;
case SDLK_s:
if(!sPressed){
selection = Math.min(selection + 1, 1);
sPressed = true;
updateButtons();
}
break;
}
break;
case SDL_KEYUP:
switch(Main.e.key.keysym.sym){
case SDLK_RETURN:
enterPressed = false;
break;
case SDLK_w:
wPressed = false;
break;
case SDLK_s:
sPressed = false;
break;
}
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, Screen.wallpaper, null, null);
SDL_RenderCopy(renderer, background, Screen.canvas, Screen.canvasPos);
SDL_RenderCopy(renderer, startTexture, buttonSize, startPos);
SDL_RenderCopy(renderer, exitTexture, buttonSize, exitPos);
SDL_RenderPresent(renderer);
}
private void updateButtons() throws Exception{
switch(selection){
case 0:
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("startsel.bmp"));
startTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("exit.bmp"));
exitTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
break;
case 1:
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("start.bmp"));
startTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("exitsel.bmp"));
exitTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
break;
}
}
private void confirm() throws Exception{
switch(selection){
case 0:
Main.scenes.push(new Game());
break;
case 1:
Main.scenes.pop();
break;
}
}
}

View File

@ -19,7 +19,9 @@ package com.fmudanyali.scenes;
import com.fmudanyali.Main;
import com.fmudanyali.Screen;
import com.fmudanyali.FileLoader;
import org.libsdl.api.rect.SDL_Rect;
import org.libsdl.api.render.*;
import org.libsdl.api.surface.SDL_Surface;
@ -30,18 +32,36 @@ import static org.libsdl.api.event.SdlEvents.*;
import static org.libsdl.api.keycode.SDL_Keycode.*;
public class PauseMenu extends Scene {
public static SDL_Surface textureSurface =
SDL_CreateRGBSurface(0, Screen.WIDTH, Screen.HEIGHT, 32, 0, 0, 0, 0);
public static SDL_Texture menuTexture;
public static boolean escPressed = false;
private SDL_Surface tempSurface;
private SDL_Texture contTexture, exitTexture, background;
private SDL_Rect contPos = new SDL_Rect();
private SDL_Rect exitPos = new SDL_Rect();
private SDL_Rect buttonSize = new SDL_Rect();
private boolean escPressed = false;
private boolean enterPressed = false;
private boolean wPressed = false;
private boolean sPressed = false;
private int selection = 0;
public PauseMenu(){
SDL_FillRect(textureSurface, null, 0);
menuTexture = SDL_CreateTextureFromSurface(renderer, textureSurface);
public PauseMenu() throws Exception{
buttonSize.x = buttonSize.y = 0;
buttonSize.w = contPos.w = exitPos.w = 150;
buttonSize.h = contPos.h = exitPos.h = 100;
contPos.x = (Screen.WIDTH - Screen.CANV_W)/2 + 10;
contPos.y = (Screen.HEIGHT - Screen.CANV_H)/2 + 10;
exitPos.x = (Screen.WIDTH - Screen.CANV_W)/2 + 10;
exitPos.y = (Screen.HEIGHT - Screen.CANV_H)/2 + 120;
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("pause.bmp"));
background = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("continuesel.bmp"));
contTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("exit.bmp"));
exitTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
}
@Override
public void loop(){
public void loop() throws Exception{
while(SDL_PollEvent(Main.e) != 0){
switch(Main.e.type){
case SDL_QUIT:
@ -55,6 +75,26 @@ public class PauseMenu extends Scene {
escPressed = true;
}
break;
case SDLK_RETURN:
if(!enterPressed){
confirm();
enterPressed = true;
}
break;
case SDLK_w:
if(!wPressed){
selection = Math.min(selection - 1, 1);
wPressed = true;
updateButtons();
}
break;
case SDLK_s:
if(!sPressed){
selection = Math.min(selection + 1, 1);
sPressed = true;
updateButtons();
}
break;
}
break;
case SDL_KEYUP:
@ -62,13 +102,55 @@ public class PauseMenu extends Scene {
case SDLK_ESCAPE:
escPressed = false;
break;
case SDLK_RETURN:
enterPressed = false;
break;
case SDLK_w:
wPressed = false;
break;
case SDLK_s:
sPressed = false;
break;
}
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, menuTexture, null, null);
SDL_RenderCopy(renderer, Screen.wallpaper, null, null);
SDL_RenderCopy(renderer, background, Screen.canvas, Screen.canvasPos);
SDL_RenderCopy(renderer, contTexture, buttonSize, contPos);
SDL_RenderCopy(renderer, exitTexture, buttonSize, exitPos);
SDL_RenderPresent(renderer);
}
private void updateButtons() throws Exception{
switch(selection){
case 0:
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("continuesel.bmp"));
contTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("exit.bmp"));
exitTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
break;
case 1:
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("continue.bmp"));
contTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
tempSurface = SDL_LoadBMP(FileLoader.getFilePath("exitsel.bmp"));
exitTexture = SDL_CreateTextureFromSurface(renderer, tempSurface);
break;
}
}
private void confirm(){
switch(selection){
case 0:
Main.scenes.pop();
Game.escPressed = false;
break;
case 1:
Main.scenes.pop();
Main.scenes.pop();
break;
}
}
}

View File

@ -18,5 +18,5 @@
package com.fmudanyali.scenes;
public abstract class Scene {
public abstract void loop();
public abstract void loop() throws Exception;
}