diff --git a/pom.xml b/pom.xml index 846790d..ccf472f 100644 --- a/pom.xml +++ b/pom.xml @@ -65,7 +65,7 @@ true - com.fmudanyali.Test + com.fmudanyali.Main diff --git a/src/main/java/com/fmudanyali/Main.java b/src/main/java/com/fmudanyali/Main.java new file mode 100644 index 0000000..ae02b06 --- /dev/null +++ b/src/main/java/com/fmudanyali/Main.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Furkan Mudanyali + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fmudanyali; + +import com.fmudanyali.scenes.*; +import java.util.Stack; +import org.libsdl.api.event.events.SDL_Event; + +public class Main { + public static SDL_Event e = new SDL_Event(); + public static boolean exit = false; + public static Stack scenes = new Stack<>(); + public static void main(String[] args) throws Exception{ + if (RestartJVM.restartJVM()) { + return; + } + + scenes.push(new Game()); + + while(!scenes.empty() && !exit){ + Time.Tick(); + Keyboard.getKeyboardState(); + scenes.peek().loop(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/fmudanyali/Render.java b/src/main/java/com/fmudanyali/Render.java index d14f6b6..8c30bc4 100644 --- a/src/main/java/com/fmudanyali/Render.java +++ b/src/main/java/com/fmudanyali/Render.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2021 Furkan Mudanyali + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package com.fmudanyali; import com.sun.jna.ptr.IntByReference; @@ -5,11 +22,21 @@ import com.sun.jna.ptr.IntByReference; import org.libsdl.api.render.SDL_Renderer; import org.libsdl.api.render.SDL_Texture; import org.libsdl.api.rect.SDL_Rect; +import org.libsdl.api.video.SDL_Window; import static org.libsdl.api.render.SdlRender.*; import static org.libsdl.api.pixels.SDL_PixelFormatEnum.*; +import static org.libsdl.api.video.SDL_WindowFlags.*; +import static org.libsdl.api.video.SdlVideo.*; public class Render { + public static int WIDTH = 960; + public static int HEIGHT = 540; + + public static SDL_Window window = SDL_CreateWindow("SDL Java Test", + SDL_WINDOWPOS_CENTERED(), SDL_WINDOWPOS_CENTERED(), + WIDTH, HEIGHT, SDL_WINDOW_SHOWN); + public static SDL_Texture createBackgroundFromTexture( SDL_Renderer renderer, SDL_Texture texture, int cols, int rows ){ diff --git a/src/main/java/com/fmudanyali/Renderer.java b/src/main/java/com/fmudanyali/Renderer.java deleted file mode 100644 index 6b3aa59..0000000 --- a/src/main/java/com/fmudanyali/Renderer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.fmudanyali; - - -import java.util.Stack; -import org.libsdl.api.render.SDL_Renderer; - -import static org.libsdl.api.Sdl.*; -import static org.libsdl.api.render.SdlRender.*; - -public class Renderer { - public static Stack renderStack = new Stack<>(); - - public static enum GameState{ - MAIN_MENU, - SETTINGS, - GAME - } - - public static void back(){ - SDL_DestroyRenderer(renderStack.peek()); - renderStack.pop(); - - if(renderStack.empty()){ - SDL_Quit(); - } - } - - public static void initialize(){ - renderStack.push( - SDL_CreateRenderer(Game.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC) - ); - - } -} diff --git a/src/main/java/com/fmudanyali/Game.java b/src/main/java/com/fmudanyali/scenes/Game.java similarity index 51% rename from src/main/java/com/fmudanyali/Game.java rename to src/main/java/com/fmudanyali/scenes/Game.java index 68472b5..28b6792 100644 --- a/src/main/java/com/fmudanyali/Game.java +++ b/src/main/java/com/fmudanyali/scenes/Game.java @@ -15,29 +15,29 @@ * along with this program. If not, see . */ -package com.fmudanyali; +package com.fmudanyali.scenes; +import com.fmudanyali.FileLoader; +import com.fmudanyali.Keyboard; +import com.fmudanyali.Main; +import com.fmudanyali.Render; +import com.fmudanyali.Time; import com.sun.jna.ptr.IntByReference; import org.libsdl.api.render.*; -import org.libsdl.api.video.SDL_Window; import org.libsdl.api.surface.SDL_Surface; import org.libsdl.api.rect.SDL_Rect; -import org.libsdl.api.event.events.SDL_Event; import static org.libsdl.api.render.SdlRender.*; import static org.libsdl.api.surface.SdlSurface.*; -import static org.libsdl.api.video.SDL_WindowFlags.*; import static org.libsdl.api.video.SdlVideo.*; -import static org.libsdl.api.SDL_SubSystem.*; import static org.libsdl.api.Sdl.*; import static org.libsdl.api.scancode.SDL_Scancode.*; -//import static org.libsdl.api.error.SdlError.*; import static org.libsdl.api.event.SdlEvents.*; +import static com.fmudanyali.Render.*; +import static org.libsdl.api.keycode.SDL_Keycode.*; -public class Game { - public static SDL_Window window; - public static SDL_Renderer renderer; +public class Game extends Scene { public static SDL_Texture texture, background, wallpaper, player; public static SDL_Surface textureSurface = new SDL_Surface(); @@ -45,28 +45,14 @@ public class Game { public static SDL_Rect canvas = new SDL_Rect(); public static SDL_Rect playerPos = new SDL_Rect(); - public static SDL_Event e = new SDL_Event(); public static boolean exit = false; - - public static int WIDTH = 960; - public static int HEIGHT = 540; public static int speed; + public static boolean escPressed = false; public static IntByReference bgwptr = new IntByReference(); public static IntByReference bghptr = new IntByReference(); - public static void initialize() throws Exception{ - // Initialize SDL - SDL_Init(SDL_INIT_VIDEO); - // Create Window - window = SDL_CreateWindow("SDL Java Test", - SDL_WINDOWPOS_CENTERED(), SDL_WINDOWPOS_CENTERED(), - WIDTH, HEIGHT, SDL_WINDOW_SHOWN); - - // Create Renderer - renderer = SDL_CreateRenderer(window, -1, - SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - + public Game() throws Exception{ // Create surface to load BMP into textureSurface = SDL_LoadBMP(FileLoader.getFilePath("image.bmp")); // Create texture from the surface @@ -82,7 +68,6 @@ public class Game { // Create a background double the width of the texture, used for scrolling animation background = Render.createBackgroundFromTexture(renderer, texture, 12, 30); SDL_QueryTexture(background, null, null, bgwptr, bghptr); - System.out.printf("Background: %dx%d%n", bgwptr.getValue(), bghptr.getValue()); // Dimensions of the viewport, its x and y is used for positioning viewport.x = viewport.y = 0; @@ -92,60 +77,67 @@ public class Game { canvas.y = (HEIGHT - canvas.h)/2; playerPos.x = playerPos.y = viewport.w - 16; playerPos.w = playerPos.h = 32; - System.out.printf("BG Res: %dx%d%n", bgwptr.getValue(), bghptr.getValue()); } - public static void loop(){ - while(!exit){ - Time.Tick(); - - while(SDL_PollEvent(e) != 0){ - switch(e.type){ - case SDL_QUIT: - exit = true; - break; - case SDL_KEYDOWN: + @Override + public void loop(){ + 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_ESCAPE: + if(!escPressed){ + Main.scenes.push(new PauseMenu()); + escPressed = true; + } + break; + } + break; + case SDL_KEYUP: + switch(Main.e.key.keysym.sym){ + case SDLK_ESCAPE: + escPressed = false; + break; + } break; - } } - - Keyboard.getKeyboardState(); - - if(Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){ - speed = 1; - }else{ - speed = 2; - } - - if(Keyboard.getKeyState(SDL_SCANCODE_A) | Keyboard.getKeyState(SDL_SCANCODE_LEFT)){ - playerPos.x = Math.max(playerPos.x - (int)(speed * Time.deltaTime * 0.1), canvas.x); - } - if(Keyboard.getKeyState(SDL_SCANCODE_D) | Keyboard.getKeyState(SDL_SCANCODE_RIGHT)){ - playerPos.x = Math.min(playerPos.x + (int)(speed * Time.deltaTime * 0.1), canvas.x + canvas.w - 32); - } - if(Keyboard.getKeyState(SDL_SCANCODE_W) | Keyboard.getKeyState(SDL_SCANCODE_UP)){ - playerPos.y = Math.max(playerPos.y - (int)(speed * Time.deltaTime * 0.1), canvas.y); - } - if(Keyboard.getKeyState(SDL_SCANCODE_S) | Keyboard.getKeyState(SDL_SCANCODE_DOWN)){ - playerPos.y = Math.min(playerPos.y + (int)(speed * Time.deltaTime * 0.1), canvas.y + canvas.h - 32); - } - - viewport.y = Math.floorMod(viewport.y - (int)(Time.deltaTime * 0.1), bghptr.getValue() - canvas.h); - - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, wallpaper, null, null); - SDL_RenderCopy(renderer, background, viewport, canvas); - SDL_RenderCopy(renderer, player, null, playerPos); - SDL_RenderPresent(renderer); - - //System.out.printf("%d,%d%n", viewport.x, viewport.y); } + + if(Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){ + speed = 1; + }else{ + speed = 2; + } + + if(Keyboard.getKeyState(SDL_SCANCODE_A) | Keyboard.getKeyState(SDL_SCANCODE_LEFT)){ + playerPos.x = Math.max(playerPos.x - (int)(speed * Time.deltaTime * 0.1), canvas.x); + } + if(Keyboard.getKeyState(SDL_SCANCODE_D) | Keyboard.getKeyState(SDL_SCANCODE_RIGHT)){ + playerPos.x = Math.min(playerPos.x + (int)(speed * Time.deltaTime * 0.1), canvas.x + canvas.w - 32); + } + if(Keyboard.getKeyState(SDL_SCANCODE_W) | Keyboard.getKeyState(SDL_SCANCODE_UP)){ + playerPos.y = Math.max(playerPos.y - (int)(speed * Time.deltaTime * 0.1), canvas.y); + } + if(Keyboard.getKeyState(SDL_SCANCODE_S) | Keyboard.getKeyState(SDL_SCANCODE_DOWN)){ + playerPos.y = Math.min(playerPos.y + (int)(speed * Time.deltaTime * 0.1), canvas.y + canvas.h - 32); + } + + viewport.y = Math.floorMod(viewport.y - (int)(Time.deltaTime * 0.1), bghptr.getValue() - canvas.h); + + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, wallpaper, null, null); + SDL_RenderCopy(renderer, background, viewport, canvas); + SDL_RenderCopy(renderer, player, null, playerPos); + SDL_RenderPresent(renderer); } public static void quit(){ SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); + SDL_DestroyWindow(Render.window); SDL_Quit(); } } \ No newline at end of file diff --git a/src/main/java/com/fmudanyali/Test.java b/src/main/java/com/fmudanyali/scenes/MainMenu.java similarity index 73% rename from src/main/java/com/fmudanyali/Test.java rename to src/main/java/com/fmudanyali/scenes/MainMenu.java index abf4995..7776059 100644 --- a/src/main/java/com/fmudanyali/Test.java +++ b/src/main/java/com/fmudanyali/scenes/MainMenu.java @@ -15,16 +15,8 @@ * along with this program. If not, see . */ -package com.fmudanyali; +package com.fmudanyali.scenes; -public class Test { - public static void main(String[] args) throws Exception{ - if (RestartJVM.restartJVM()) { - return; - } - - Game.initialize(); - Game.loop(); - Game.quit(); - } -} \ No newline at end of file +public class MainMenu extends Scene { + +} diff --git a/src/main/java/com/fmudanyali/scenes/PauseMenu.java b/src/main/java/com/fmudanyali/scenes/PauseMenu.java new file mode 100644 index 0000000..abd654b --- /dev/null +++ b/src/main/java/com/fmudanyali/scenes/PauseMenu.java @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 Furkan Mudanyali + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fmudanyali.scenes; + +import com.fmudanyali.Main; +import com.fmudanyali.Keyboard; + +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.scancode.SDL_Scancode.*; +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, WIDTH, HEIGHT, 32, 0, 0, 0, 0); + public static SDL_Texture menuTexture; + public static boolean escPressed = false; + + public PauseMenu(){ + SDL_FillRect(textureSurface, null, 0); + menuTexture = SDL_CreateTextureFromSurface(renderer, textureSurface); + } + + @Override + public void loop(){ + 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_ESCAPE: + if(!escPressed){ + Main.scenes.pop(); + escPressed = true; + } + break; + } + break; + case SDL_KEYUP: + switch(Main.e.key.keysym.sym){ + case SDLK_ESCAPE: + escPressed = false; + break; + } + break; + } + } + + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, menuTexture, null, null); + SDL_RenderPresent(renderer); + } +} diff --git a/src/main/java/com/fmudanyali/scenes/Scene.java b/src/main/java/com/fmudanyali/scenes/Scene.java new file mode 100644 index 0000000..b8330b3 --- /dev/null +++ b/src/main/java/com/fmudanyali/scenes/Scene.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Furkan Mudanyali + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.fmudanyali.scenes; + +import com.fmudanyali.Render; + +import org.libsdl.api.render.SDL_Renderer; + +import static org.libsdl.api.Sdl.*; +import static org.libsdl.api.render.SdlRender.*; +import static org.libsdl.api.SDL_SubSystem.*; + +public class Scene { + public static SDL_Renderer renderer = + SDL_CreateRenderer(Render.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + + public void loop(){}; +} \ No newline at end of file