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>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.fmudanyali.Test</mainClass>
<mainClass>com.fmudanyali.Main</mainClass>
</manifest>
</archive>
<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;
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
){

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/>.
*/
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,25 +77,35 @@ 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){
@Override
public void loop(){
while(SDL_PollEvent(Main.e) != 0){
switch(Main.e.type){
case SDL_QUIT:
exit = true;
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{
@ -137,15 +132,12 @@ public class Game {
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);
}
}
public static void quit(){
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_DestroyWindow(Render.window);
SDL_Quit();
}
}

View File

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