diff --git a/assets/image.bmp b/assets/image.bmp deleted file mode 100644 index f118b02..0000000 Binary files a/assets/image.bmp and /dev/null differ diff --git a/assets/person.bmp b/assets/player.bmp similarity index 100% rename from assets/person.bmp rename to assets/player.bmp diff --git a/assets/tile.bmp b/assets/scene1/tile.bmp similarity index 100% rename from assets/tile.bmp rename to assets/scene1/tile.bmp diff --git a/assets/wallpaper.bmp b/assets/wallpaper.bmp new file mode 100644 index 0000000..b677e1c Binary files /dev/null and b/assets/wallpaper.bmp differ diff --git a/src/main/java/com/fmudanyali/Main.java b/src/main/java/com/fmudanyali/Main.java index ae02b06..8140f9f 100644 --- a/src/main/java/com/fmudanyali/Main.java +++ b/src/main/java/com/fmudanyali/Main.java @@ -30,6 +30,7 @@ public class Main { return; } + Screen.init(); scenes.push(new Game()); while(!scenes.empty() && !exit){ diff --git a/src/main/java/com/fmudanyali/Render.java b/src/main/java/com/fmudanyali/Render.java index 8c30bc4..7e59770 100644 --- a/src/main/java/com/fmudanyali/Render.java +++ b/src/main/java/com/fmudanyali/Render.java @@ -28,14 +28,17 @@ 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.*; +import static com.fmudanyali.Screen.*; 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_Renderer renderer = + SDL_CreateRenderer(Render.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + + public static int bgw, bgh; public static SDL_Texture createBackgroundFromTexture( SDL_Renderer renderer, SDL_Texture texture, int cols, int rows @@ -54,8 +57,8 @@ public class Render { // Remove the pointers txwptr = txhptr = null; // Calculate background resolution - int bgw = txw * cols; - int bgh = txh * rows; + bgw = txw * cols; + bgh = txh * rows; // Create background texture SDL_Texture background = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, bgw, bgh); diff --git a/src/main/java/com/fmudanyali/Screen.java b/src/main/java/com/fmudanyali/Screen.java new file mode 100644 index 0000000..fd47e3a --- /dev/null +++ b/src/main/java/com/fmudanyali/Screen.java @@ -0,0 +1,66 @@ +/* + * 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 org.libsdl.api.rect.SDL_Rect; +import org.libsdl.api.render.SDL_Texture; +import org.libsdl.api.surface.SDL_Surface; + +import static org.libsdl.api.surface.SdlSurface.*; +import static org.libsdl.api.render.SdlRender.*; + +public class Screen { + public static final int WIDTH = 960; + public static final int HEIGHT = 540; + public static final int CANV_W = 360; + public static final int CANV_H = 480; + + public static SDL_Rect canvas = new SDL_Rect(); + public static SDL_Rect canvasPos = new SDL_Rect(); + public static SDL_Texture tile, background, wallpaper; + public static SDL_Surface tempSurface; + + public static void init() throws Exception{ + canvas.x = canvas.y = 0; + canvas.h = canvasPos.h = CANV_H; + canvas.w = canvasPos.w = CANV_W; + // Center canvas to the screen + canvasPos.x = (WIDTH - CANV_W)/2; + canvasPos.y = (HEIGHT - CANV_H)/2; + // Create background wallpaper + tempSurface = SDL_LoadBMP(FileLoader.getFilePath("wallpaper.bmp")); + // Create texture from the surface + wallpaper = SDL_CreateTextureFromSurface(Render.renderer, tempSurface); + // Destroy the surface + tempSurface = null; + } + + public static void makeBackground(String filename) throws Exception{ + // Load tile + tempSurface = SDL_LoadBMP(FileLoader.getFilePath(filename)); + tile = SDL_CreateTextureFromSurface(Render.renderer, tempSurface); + // Clear surface + tempSurface = null; + // Create background from tiles + background = Render.createBackgroundFromTexture(Render.renderer, tile, 12, 30); + } + + public static void scroll(){ + canvas.y = Math.floorMod(canvas.y - (int)(Time.deltaTime * 0.1), Render.bgh - canvas.h); + } +} diff --git a/src/main/java/com/fmudanyali/characters/Character.java b/src/main/java/com/fmudanyali/characters/Character.java new file mode 100644 index 0000000..e9a8490 --- /dev/null +++ b/src/main/java/com/fmudanyali/characters/Character.java @@ -0,0 +1,28 @@ +/* + * 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.characters; + +import org.libsdl.api.rect.SDL_Rect; +import org.libsdl.api.render.SDL_Texture; +import org.libsdl.api.surface.SDL_Surface; + +public class Character { + public SDL_Rect position = new SDL_Rect(); + public SDL_Texture texture = new SDL_Texture(); + public SDL_Surface tempSurface = new SDL_Surface(); +} diff --git a/src/main/java/com/fmudanyali/characters/Enemy.java b/src/main/java/com/fmudanyali/characters/Enemy.java new file mode 100644 index 0000000..4269a11 --- /dev/null +++ b/src/main/java/com/fmudanyali/characters/Enemy.java @@ -0,0 +1,22 @@ +/* + * 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.characters; + +public class Enemy extends Character { + +} diff --git a/src/main/java/com/fmudanyali/characters/Player.java b/src/main/java/com/fmudanyali/characters/Player.java new file mode 100644 index 0000000..f9b17c8 --- /dev/null +++ b/src/main/java/com/fmudanyali/characters/Player.java @@ -0,0 +1,64 @@ +/* + * 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.characters; + +import com.fmudanyali.FileLoader; +import com.fmudanyali.Render; +import com.fmudanyali.Screen; +import com.fmudanyali.Keyboard; +import com.fmudanyali.Time; + +import static org.libsdl.api.surface.SdlSurface.*; +import static org.libsdl.api.render.SdlRender.*; +import static org.libsdl.api.scancode.SDL_Scancode.*; + +public class Player extends Character { + public int lives; + public int speed; + + public Player() throws Exception{ + lives = 3; + tempSurface = SDL_LoadBMP(FileLoader.getFilePath("player.bmp")); + texture = SDL_CreateTextureFromSurface(Render.renderer, tempSurface); + tempSurface = null; + position.x = Screen.canvasPos.x + Screen.canvas.w/2 - 16; + position.y = Screen.canvasPos.y + (int)(Screen.canvas.h/1.25) - 16; + position.w = position.h = 32; + } + + public void movement(){ + if(Keyboard.getKeyState(SDL_SCANCODE_LSHIFT)){ + speed = 1; + }else{ + speed = 2; + } + + if(Keyboard.getKeyState(SDL_SCANCODE_A) | Keyboard.getKeyState(SDL_SCANCODE_LEFT)){ + position.x = Math.max(position.x - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x); + } + if(Keyboard.getKeyState(SDL_SCANCODE_D) | Keyboard.getKeyState(SDL_SCANCODE_RIGHT)){ + position.x = Math.min(position.x + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.x + Screen.canvas.w - 32); + } + if(Keyboard.getKeyState(SDL_SCANCODE_W) | Keyboard.getKeyState(SDL_SCANCODE_UP)){ + position.y = Math.max(position.y - (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y); + } + if(Keyboard.getKeyState(SDL_SCANCODE_S) | Keyboard.getKeyState(SDL_SCANCODE_DOWN)){ + position.y = Math.min(position.y + (int)(speed * Time.deltaTime * 0.1), Screen.canvasPos.y + Screen.canvas.h - 32); + } + } +} diff --git a/src/main/java/com/fmudanyali/scenes/Game.java b/src/main/java/com/fmudanyali/scenes/Game.java index 28b6792..ec6dff8 100644 --- a/src/main/java/com/fmudanyali/scenes/Game.java +++ b/src/main/java/com/fmudanyali/scenes/Game.java @@ -17,66 +17,23 @@ 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 com.fmudanyali.Screen; +import com.fmudanyali.characters.Player; -import org.libsdl.api.render.*; -import org.libsdl.api.surface.SDL_Surface; -import org.libsdl.api.rect.SDL_Rect; - -import static org.libsdl.api.render.SdlRender.*; -import static org.libsdl.api.surface.SdlSurface.*; -import static org.libsdl.api.video.SdlVideo.*; -import static org.libsdl.api.Sdl.*; -import static org.libsdl.api.scancode.SDL_Scancode.*; 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.*; public class Game extends Scene { - public static SDL_Texture texture, background, wallpaper, player; - public static SDL_Surface textureSurface = new SDL_Surface(); - - public static SDL_Rect viewport = new SDL_Rect(); - public static SDL_Rect canvas = new SDL_Rect(); - public static SDL_Rect playerPos = new SDL_Rect(); - public static boolean exit = false; - public static int speed; public static boolean escPressed = false; - public static IntByReference bgwptr = new IntByReference(); - public static IntByReference bghptr = new IntByReference(); + public Player player = new Player(); public Game() throws Exception{ - // Create surface to load BMP into - textureSurface = SDL_LoadBMP(FileLoader.getFilePath("image.bmp")); - // Create texture from the surface - wallpaper = SDL_CreateTextureFromSurface(renderer, textureSurface); - // Destroy the surface - textureSurface = null; - textureSurface = SDL_LoadBMP(FileLoader.getFilePath("tile.bmp")); - texture = SDL_CreateTextureFromSurface(renderer, textureSurface); - textureSurface = null; - textureSurface = SDL_LoadBMP(FileLoader.getFilePath("person.bmp")); - player = SDL_CreateTextureFromSurface(renderer, textureSurface); - textureSurface = null; - // 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); - - // Dimensions of the viewport, its x and y is used for positioning - viewport.x = viewport.y = 0; - viewport.w = canvas.w = 384; - viewport.h = canvas.h = 480; - canvas.x = (WIDTH - canvas.w)/2; - canvas.y = (HEIGHT - canvas.h)/2; - playerPos.x = playerPos.y = viewport.w - 16; - playerPos.w = playerPos.h = 32; + Screen.makeBackground("scene1/tile.bmp"); } @Override @@ -106,38 +63,13 @@ public class Game extends Scene { } } - 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); + player.movement(); + Screen.scroll(); SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, wallpaper, null, null); - SDL_RenderCopy(renderer, background, viewport, canvas); - SDL_RenderCopy(renderer, player, null, playerPos); + SDL_RenderCopy(renderer, Screen.wallpaper, null, null); + SDL_RenderCopy(renderer, Screen.background, Screen.canvas, Screen.canvasPos); + SDL_RenderCopy(renderer, player.texture, null, player.position); SDL_RenderPresent(renderer); } - - public static void quit(){ - SDL_DestroyTexture(texture); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(Render.window); - SDL_Quit(); - } } \ No newline at end of file diff --git a/src/main/java/com/fmudanyali/scenes/MainMenu.java b/src/main/java/com/fmudanyali/scenes/MainMenu.java index 7776059..17b0bd9 100644 --- a/src/main/java/com/fmudanyali/scenes/MainMenu.java +++ b/src/main/java/com/fmudanyali/scenes/MainMenu.java @@ -18,5 +18,5 @@ package com.fmudanyali.scenes; public class MainMenu extends Scene { - + public void loop(){}; } diff --git a/src/main/java/com/fmudanyali/scenes/PauseMenu.java b/src/main/java/com/fmudanyali/scenes/PauseMenu.java index abd654b..18d83da 100644 --- a/src/main/java/com/fmudanyali/scenes/PauseMenu.java +++ b/src/main/java/com/fmudanyali/scenes/PauseMenu.java @@ -18,7 +18,7 @@ package com.fmudanyali.scenes; import com.fmudanyali.Main; -import com.fmudanyali.Keyboard; +import com.fmudanyali.Screen; import org.libsdl.api.render.*; import org.libsdl.api.surface.SDL_Surface; @@ -26,13 +26,12 @@ 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); + SDL_CreateRGBSurface(0, Screen.WIDTH, Screen.HEIGHT, 32, 0, 0, 0, 0); public static SDL_Texture menuTexture; public static boolean escPressed = false; diff --git a/src/main/java/com/fmudanyali/scenes/Scene.java b/src/main/java/com/fmudanyali/scenes/Scene.java index b8330b3..1c2ba21 100644 --- a/src/main/java/com/fmudanyali/scenes/Scene.java +++ b/src/main/java/com/fmudanyali/scenes/Scene.java @@ -17,17 +17,6 @@ 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(){}; +public abstract class Scene { + public abstract void loop(); } \ No newline at end of file