Add scene implementation

This commit is contained in:
Furkan Mudanyali 2021-12-04 18:16:25 +03:00
parent 9c8b6f0a01
commit 94722b4833
8 changed files with 243 additions and 117 deletions

View File

@ -65,7 +65,7 @@
<archive> <archive>
<manifest> <manifest>
<addClasspath>true</addClasspath> <addClasspath>true</addClasspath>
<mainClass>com.fmudanyali.Test</mainClass> <mainClass>com.fmudanyali.Main</mainClass>
</manifest> </manifest>
</archive> </archive>
<descriptorRefs> <descriptorRefs>

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<Scene> 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();
}
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
package com.fmudanyali; package com.fmudanyali;
import com.sun.jna.ptr.IntByReference; 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_Renderer;
import org.libsdl.api.render.SDL_Texture; import org.libsdl.api.render.SDL_Texture;
import org.libsdl.api.rect.SDL_Rect; 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.render.SdlRender.*;
import static org.libsdl.api.pixels.SDL_PixelFormatEnum.*; 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 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( public static SDL_Texture createBackgroundFromTexture(
SDL_Renderer renderer, SDL_Texture texture, int cols, int rows SDL_Renderer renderer, SDL_Texture texture, int cols, int rows
){ ){

View File

@ -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<SDL_Renderer> 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)
);
}
}

View File

@ -15,29 +15,29 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 com.sun.jna.ptr.IntByReference;
import org.libsdl.api.render.*; import org.libsdl.api.render.*;
import org.libsdl.api.video.SDL_Window;
import org.libsdl.api.surface.SDL_Surface; import org.libsdl.api.surface.SDL_Surface;
import org.libsdl.api.rect.SDL_Rect; 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.render.SdlRender.*;
import static org.libsdl.api.surface.SdlSurface.*; 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.video.SdlVideo.*;
import static org.libsdl.api.SDL_SubSystem.*;
import static org.libsdl.api.Sdl.*; import static org.libsdl.api.Sdl.*;
import static org.libsdl.api.scancode.SDL_Scancode.*; import static org.libsdl.api.scancode.SDL_Scancode.*;
//import static org.libsdl.api.error.SdlError.*;
import static org.libsdl.api.event.SdlEvents.*; import static org.libsdl.api.event.SdlEvents.*;
import static com.fmudanyali.Render.*;
import static org.libsdl.api.keycode.SDL_Keycode.*;
public class Game { public class Game extends Scene {
public static SDL_Window window;
public static SDL_Renderer renderer;
public static SDL_Texture texture, background, wallpaper, player; public static SDL_Texture texture, background, wallpaper, player;
public static SDL_Surface textureSurface = new SDL_Surface(); 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 canvas = new SDL_Rect();
public static SDL_Rect playerPos = 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 boolean exit = false;
public static int WIDTH = 960;
public static int HEIGHT = 540;
public static int speed; public static int speed;
public static boolean escPressed = false;
public static IntByReference bgwptr = new IntByReference(); public static IntByReference bgwptr = new IntByReference();
public static IntByReference bghptr = new IntByReference(); public static IntByReference bghptr = new IntByReference();
public static void initialize() throws Exception{ public Game() 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);
// Create surface to load BMP into // Create surface to load BMP into
textureSurface = SDL_LoadBMP(FileLoader.getFilePath("image.bmp")); textureSurface = SDL_LoadBMP(FileLoader.getFilePath("image.bmp"));
// Create texture from the surface // 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 // Create a background double the width of the texture, used for scrolling animation
background = Render.createBackgroundFromTexture(renderer, texture, 12, 30); background = Render.createBackgroundFromTexture(renderer, texture, 12, 30);
SDL_QueryTexture(background, null, null, bgwptr, bghptr); 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 // Dimensions of the viewport, its x and y is used for positioning
viewport.x = viewport.y = 0; viewport.x = viewport.y = 0;
@ -92,60 +77,67 @@ public class Game {
canvas.y = (HEIGHT - canvas.h)/2; canvas.y = (HEIGHT - canvas.h)/2;
playerPos.x = playerPos.y = viewport.w - 16; playerPos.x = playerPos.y = viewport.w - 16;
playerPos.w = playerPos.h = 32; playerPos.w = playerPos.h = 32;
System.out.printf("BG Res: %dx%d%n", bgwptr.getValue(), bghptr.getValue());
} }
public static void loop(){ @Override
while(!exit){ public void loop(){
Time.Tick(); while(SDL_PollEvent(Main.e) != 0){
switch(Main.e.type){
while(SDL_PollEvent(e) != 0){ case SDL_QUIT:
switch(e.type){ Main.exit = true;
case SDL_QUIT: break;
exit = true; case SDL_KEYDOWN:
break; switch(Main.e.key.keysym.sym){
case SDL_KEYDOWN: 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; 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(){ public static void quit(){
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(Render.window);
SDL_Quit(); SDL_Quit();
} }
} }

View File

@ -15,16 +15,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package com.fmudanyali; package com.fmudanyali.scenes;
public class Test { public class MainMenu extends Scene {
public static void main(String[] args) throws Exception{
if (RestartJVM.restartJVM()) { }
return;
}
Game.initialize();
Game.loop();
Game.quit();
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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(){};
}