Divided stuff into classes, new wallpaper

This commit is contained in:
Furkan Mudanyali 2021-12-08 00:30:33 +03:00
parent 94722b4833
commit 5833de18f4
14 changed files with 204 additions and 100 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 900 KiB

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
assets/wallpaper.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

View File

@ -30,6 +30,7 @@ public class Main {
return;
}
Screen.init();
scenes.push(new Game());
while(!scenes.empty() && !exit){

View File

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

View File

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

View File

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

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
package com.fmudanyali.characters;
public class Enemy extends Character {
}

View File

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

View File

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

View File

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

View File

@ -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;

View File

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