added scaling

This commit is contained in:
MasterGordon 2022-10-05 15:49:40 +02:00
parent 1120f68e93
commit bf3ad4a1b0
6 changed files with 55 additions and 53 deletions

View File

@ -1,4 +1,4 @@
var window = new Window("ASDLteroids", 800, 600); var window = new Window("ASDLteroids", (int)(800 * Scene.SCALE), (int)(600 * Scene.SCALE));
var renderer = new Renderer(window); var renderer = new Renderer(window);
var scene = new Scene(renderer); var scene = new Scene(renderer);

View File

@ -2,8 +2,9 @@ using static SDL2.SDL;
class Scene class Scene
{ {
public const int SCREEN_WIDTH = 800; public const double SCALE = 1.5;
public const int SCREEN_HEIGHT = 600; public const int SCREEN_WIDTH = (int)(800 * SCALE);
public const int SCREEN_HEIGHT = (int)(600 * SCALE);
private Ship ship; private Ship ship;
private UI ui; private UI ui;
private Renderer renderer; private Renderer renderer;

View File

@ -7,7 +7,7 @@ enum Size : int
class Asteroid : Renderable, Logic class Asteroid : Renderable, Logic
{ {
const double SPEED = 100; const double SPEED = 100 * Scene.SCALE;
private double dx, dy; private double dx, dy;
public double X, Y; public double X, Y;
public Size Size; public Size Size;
@ -26,8 +26,8 @@ class Asteroid : Renderable, Logic
{ {
var rad = i * Math.PI * 2 / shape.Length; var rad = i * Math.PI * 2 / shape.Length;
shape[i] = new double[2]; shape[i] = new double[2];
shape[i][0] = Math.Cos(rad) * (0.5 + random.NextDouble() / 2) * (int)size / 2; shape[i][0] = Math.Cos(rad) * (0.5 + random.NextDouble() / 2) * (int)size / 2 * Scene.SCALE;
shape[i][1] = Math.Sin(rad) * (0.5 + random.NextDouble() / 2) * (int)size / 2; shape[i][1] = Math.Sin(rad) * (0.5 + random.NextDouble() / 2) * (int)size / 2 * Scene.SCALE;
} }
} }
@ -35,15 +35,16 @@ class Asteroid : Renderable, Logic
{ {
renderer.setColor(255, 255, 255); renderer.setColor(255, 255, 255);
double[][] drawLines = new double[shape.Length + 1][]; double[][] drawLines = new double[shape.Length + 1][];
var halfSize = (int)Size / 2 * Scene.SCALE;
for (int i = 0; i < shape.Length; i++) for (int i = 0; i < shape.Length; i++)
{ {
drawLines[i] = new double[2]; drawLines[i] = new double[2];
drawLines[i][0] = shape[i][0] + X - (int)Size / 2; drawLines[i][0] = shape[i][0] + X - halfSize;
drawLines[i][1] = shape[i][1] + Y - (int)Size / 2; drawLines[i][1] = shape[i][1] + Y - halfSize;
} }
drawLines[drawLines.Length - 1] = new double[2]; drawLines[drawLines.Length - 1] = new double[2];
drawLines[drawLines.Length - 1][0] = shape[0][0] + X - (int)Size / 2; drawLines[drawLines.Length - 1][0] = shape[0][0] + X - halfSize;
drawLines[drawLines.Length - 1][1] = shape[0][1] + Y - (int)Size / 2; drawLines[drawLines.Length - 1][1] = shape[0][1] + Y - halfSize;
renderer.DrawLines(drawLines); renderer.DrawLines(drawLines);
} }
@ -64,8 +65,8 @@ class Asteroid : Renderable, Logic
foreach (var shot in Scene.Instance.Shots) foreach (var shot in Scene.Instance.Shots)
{ {
var halfSize = (int)Size / 2; var halfSize = (int)Size / 2 * Scene.SCALE;
if (Point.Distance(shot.X, shot.Y, X - halfSize, Y - halfSize) < (int)Size / 2) if (Point.Distance(shot.X, shot.Y, X - halfSize, Y - halfSize) < halfSize)
{ {
shot.Destroy(); shot.Destroy();
this.Destroy(); this.Destroy();

View File

@ -1,6 +1,6 @@
class Ship : Logic, Renderable class Ship : Logic, Renderable
{ {
const double SPEED = 3; const double SPEED = 3 * Scene.SCALE;
const double ROTATION_SPEED = 5; const double ROTATION_SPEED = 5;
double rotation = 0; double rotation = 0;
@ -61,8 +61,8 @@ class Ship : Logic, Renderable
} }
foreach (var asteroid in Scene.Instance.Asteroids) foreach (var asteroid in Scene.Instance.Asteroids)
{ {
var halfAsteroidSize = (int)asteroid.Size / 2; var halfAsteroidSize = (int)asteroid.Size / 2 * Scene.SCALE;
if (Point.Distance(x, y, asteroid.X - halfAsteroidSize, asteroid.Y - halfAsteroidSize) < halfAsteroidSize + 3) if (Point.Distance(x, y, asteroid.X - halfAsteroidSize, asteroid.Y - halfAsteroidSize) < halfAsteroidSize)
{ {
Scene.Instance.Loose(); Scene.Instance.Loose();
} }
@ -73,14 +73,14 @@ class Ship : Logic, Renderable
{ {
renderer.setColor(255, 255, 255); renderer.setColor(255, 255, 255);
renderer.DrawLines(new double[][] { renderer.DrawLines(new double[][] {
new double[] { x + Math.Cos(rotation) * 10, y + Math.Sin(rotation) * 10 }, new double[] { x + Math.Cos(rotation) * 10 * Scene.SCALE, y + Math.Sin(rotation) * 10 * Scene.SCALE },
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3) * 10, y + Math.Sin(rotation + (Math.PI * 2) / 3) * 10 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3) * 10 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3) * 10 * Scene.SCALE},
new double[] { x + Math.Cos(rotation) * 10, y + Math.Sin(rotation) * 10 }, new double[] { x + Math.Cos(rotation) * 10 * Scene.SCALE, y + Math.Sin(rotation) * 10 * Scene.SCALE},
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 2) * 10, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 2) * 10 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 2) * 10 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 2) * 10 * Scene.SCALE },
}); });
renderer.DrawLines(new double[][] { renderer.DrawLines(new double[][] {
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3) * 5, y + Math.Sin(rotation + (Math.PI * 2) / 3) * 5 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3) * 5 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3) * 5 * Scene.SCALE },
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 2) * 5, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 2) * 5 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 2) * 5 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 2) * 5 * Scene.SCALE },
}); });
if (thrust) if (thrust)
@ -89,12 +89,12 @@ class Ship : Logic, Renderable
var random = new Random(); var random = new Random();
// Draw flame behind ship // Draw flame behind ship
renderer.DrawLines(new double[][] { renderer.DrawLines(new double[][] {
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3) * 10, y + Math.Sin(rotation + (Math.PI * 2) / 3) * 10 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3) * 10 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3) * 10 * Scene.SCALE},
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.2) * 5+random.Next(5), y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.2) * 5+random.Next(5) }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.2) * 5+random.Next(5) * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.2) * 5+random.Next(5) * Scene.SCALE },
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.4) * 10, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.4) * 10 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.4) * 10 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.4) * 10 * Scene.SCALE },
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.6) * 6+random.Next(5), y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.6) * 5+random.Next(4) }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.6) * 6+random.Next(5) * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.6) * 5+random.Next(4) * Scene.SCALE },
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.8) * 10, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.8) * 10 }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 1.8) * 10 * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 1.8) * 10 * Scene.SCALE },
new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 2) * 5+random.Next(5), y + Math.Sin(rotation + (Math.PI * 2) / 3 * 2) * 5+random.Next(5) }, new double[] { x + Math.Cos(rotation + (Math.PI * 2) / 3 * 2) * 5+random.Next(5) * Scene.SCALE, y + Math.Sin(rotation + (Math.PI * 2) / 3 * 2) * 5+random.Next(5) * Scene.SCALE },
}); });
} }
} }

View File

@ -1,6 +1,6 @@
class Shot : Renderable, Logic class Shot : Renderable, Logic
{ {
const double SPEED = 300; const double SPEED = 300 * Scene.SCALE;
private double dx, dy; private double dx, dy;
public double X, Y; public double X, Y;

View File

@ -4,10 +4,16 @@ using static SDL2.SDL;
class UI : Renderable class UI : Renderable
{ {
IntPtr font; IntPtr font;
SDL_Color color;
public UI() public UI()
{ {
TTF_Init(); TTF_Init();
color = new SDL_Color();
color.r = 255;
color.g = 255;
color.b = 255;
color.a = 255;
var assemblyName = this.GetType().Assembly.GetName().Name!; var assemblyName = this.GetType().Assembly.GetName().Name!;
var fontStream = this.GetType().Assembly.GetManifestResourceStream($"{assemblyName}.assets.font.ttf"); var fontStream = this.GetType().Assembly.GetManifestResourceStream($"{assemblyName}.assets.font.ttf");
var fontFile = System.IO.Path.GetTempPath() + "asdlteroids-font.ttf"; var fontFile = System.IO.Path.GetTempPath() + "asdlteroids-font.ttf";
@ -21,12 +27,18 @@ class UI : Renderable
public void Render(Renderer renderer, double dx) public void Render(Renderer renderer, double dx)
{ {
var white = new SDL_Color(); RenderText(renderer, "Score: " + Scene.Instance.Score, 0, 0);
white.r = 255; RenderText(renderer, "Level: " + Scene.Instance.Level, 0, 20);
white.g = 255;
white.b = 255; if (!Scene.Instance.running && DateTime.Now.Second % 2 == 0)
white.a = 255; {
var surfaceMessage = TTF_RenderText_Solid(this.font, "Score: " + Scene.Instance.Score, white); RenderText(renderer, "Press r to restart", Scene.SCREEN_WIDTH / 2, Scene.SCREEN_HEIGHT / 2, true);
}
}
public void RenderText(Renderer renderer, String text, int x, int y, bool center = false)
{
var surfaceMessage = TTF_RenderText_Solid(this.font, text, this.color);
var texture = SDL_CreateTextureFromSurface(renderer.GetRaw(), surfaceMessage); var texture = SDL_CreateTextureFromSurface(renderer.GetRaw(), surfaceMessage);
int width; int width;
@ -35,29 +47,17 @@ class UI : Renderable
SDL_QueryTexture(texture, out _, out _, out width, out height); SDL_QueryTexture(texture, out _, out _, out width, out height);
SDL_Rect rect = new SDL_Rect(); SDL_Rect rect = new SDL_Rect();
rect.x = 0; rect.x = x;
rect.y = 0; rect.y = y;
rect.w = width; rect.w = width;
rect.h = height; rect.h = height;
SDL_RenderCopy(renderer.GetRaw(), texture, IntPtr.Zero, ref rect); if (center)
if (!Scene.Instance.running)
{ {
var surfaceMessage2 = TTF_RenderText_Solid(this.font, "Press r to restart", white); rect.x -= width / 2;
var texture2 = SDL_CreateTextureFromSurface(renderer.GetRaw(), surfaceMessage2); rect.y -= height / 2;
int width2; }
int height2;
SDL_QueryTexture(texture2, out _, out _, out width2, out height2); SDL_RenderCopy(renderer.GetRaw(), texture, IntPtr.Zero, ref rect);
SDL_Rect rect2 = new SDL_Rect();
rect2.x = Scene.SCREEN_WIDTH / 2 - width2 / 2;
rect2.y = Scene.SCREEN_HEIGHT / 2 - height2 / 2;
rect2.w = width2;
rect2.h = height2;
SDL_RenderCopy(renderer.GetRaw(), texture2, IntPtr.Zero, ref rect2);
}
} }
} }