Add window scaling, TODO: fix slowdown

This commit is contained in:
Furkan Mudanyali 2021-12-08 18:11:08 +03:00
parent 5833de18f4
commit 4832492f1e
3 changed files with 29 additions and 9 deletions

View File

@ -29,7 +29,7 @@ public class Main {
if (RestartJVM.restartJVM()) {
return;
}
Render.init();
Screen.init();
scenes.push(new Game());

View File

@ -29,17 +29,28 @@ 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.*;
import static org.libsdl.api.hints.SdlHintsConst.*;
import static org.libsdl.api.hints.SdlHints.*;
import static org.libsdl.api.render.SdlRender.SDL_RenderSetLogicalSize;
public class Render {
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 SDL_Window window;
public static SDL_Renderer renderer;
public static int bgw, bgh;
public static void init(){
window = SDL_CreateWindow("DanmakuProject SDL",
SDL_WINDOWPOS_CENTERED(), SDL_WINDOWPOS_CENTERED(),
WIDTH, HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
renderer =
SDL_CreateRenderer(Render.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_RenderSetLogicalSize(renderer, Screen.WIDTH, Screen.HEIGHT);
}
public static SDL_Texture createBackgroundFromTexture(
SDL_Renderer renderer, SDL_Texture texture, int cols, int rows
){
@ -61,7 +72,7 @@ public class Render {
bgh = txh * rows;
// Create background texture
SDL_Texture background = SDL_CreateTexture(
renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, bgw, bgh);
renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, bgw, bgh);
// Set render target to background
SDL_SetRenderTarget(renderer, background);
// Create position rectangle for the texture

View File

@ -25,10 +25,12 @@ 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.*;
import static org.libsdl.api.error.SdlError.SDL_GetError;
public class Game extends Scene {
public static boolean exit = false;
public static boolean escPressed = false;
public int kek = 0;
public Player player = new Player();
@ -65,6 +67,13 @@ public class Game extends Scene {
player.movement();
Screen.scroll();
/*
++kek;
if(kek == 60){
System.out.println(SDL_GetError());
kek = 0;
}
*/
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, Screen.wallpaper, null, null);