Strategy game, pixel art and an OpenGL window (Post #10 / Strategy game #1)

In preparation for my first VB.Net Blog game project, a civilization-like strategy game, I'll create some base tiles and some units.

We'll take these sprite sheets and program the strategy game logic, AI and control from start to finish.

I'm not an artist, and that's a fact, but I'll try my best to create some at least mediocre pixel art.

Sprite Sheet #1 (Tilemap)

Here we'll store all our tile sprites, and our over-tile sprites (rivers, farming, waste, etc.)

 So our tile identification indexing will be the following

Sprite Sheet #2 (Units)

Our units size is 64x56 (Y offset of 24 pixels)


Console application

Since we'll start working on a game tutorial, I'll post here the code to create a rendering window without win-forms.

 (Start a new project - console application) 

Then create a class (I hate classes that's why you won't see lots of classes in my code, that's also the main reason I don't program in C#).

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class C_Render
    Inherits GameWindow
    Public Sub New()
        MyBase.New(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, New OpenTK.Graphics.GraphicsMode(32, 8, 0, 0))
        Dim SPoint As Point = New Point(0, 0)
        MyBase.Location = SPoint
        MyBase.VSync = True
        MyBase.WindowBorder = OpenTK.WindowBorder.Hidden
        'MyBase.WindowState = OpenTK.WindowState.Maximized
        MyBase.WindowState = OpenTK.WindowState.Fullscreen
    End Sub
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) 'AS YOU WOULD HAVE WINDOW LOAD
        RenderF("PRELOAD")
    End Sub
    Protected Overrides Sub OnRenderFrame(ByVal e As OpenTK.FrameEventArgs)
        RenderF("INVALIDATE")
        SwapBuffers()
    End Sub
    Protected Overrides Sub OnKeyDown(ByVal e As OpenTK.Input.KeyboardKeyEventArgs)
        MyBase.OnKeyDown(e)
        End
    End Sub
End Class

That's our class for now. And on our Main module the code will be like this.

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Module M_Game

    Public sRect As Rectangle
    Public dRect As Rectangle

    Public RotateX, RotateY, RotateZ As Integer
    Public TranslateX, TranslateY, TranslateZ As Single

    Public ProjectionType As String = "ORTHOGRAPHIC" '"PERSPECTIVE" '"ORTHOGRAPHIC"
    Public ScreenWidth As Integer = 960
    Public ScreenHeight As Integer = 540

    Public Pitch, Yaw, Roll As Single
    Public FOV As Single = 1.24F

    Public Sub Main()

        Dim RenderWindow As New C_Render
        RenderWindow.Run(60, 60) 'No Shadow

        RenderF("PRELOAD")
        'GridCalls("CREATE")
        'LoadTextures()
    End Sub


    Public Sub RenderF(ByVal Func As String)
        If Func = "PRELOAD" Then
            GL.Enable(EnableCap.Blend) 'Enable transparency
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha) 'Blend Functionality
            GL.Enable(EnableCap.DepthTest) 'Enabling depth
            GL.DepthFunc(DepthFunction.Lequal) 'First 0, Last 1
            GL.Enable(EnableCap.Texture2D)
            GL.Enable(EnableCap.AlphaTest) 'Edge hardiness
            GL.AlphaFunc(AlphaFunction.Gequal, 0.7F)
            GL.ShadeModel(ShadingModel.Smooth) 'Smooth shading
            GL.ClearColor(Color.Black)
        ElseIf Func = "INVALIDATE" Then
            GL.Clear(ClearBufferMask.ColorBufferBit)
            GL.Clear(ClearBufferMask.DepthBufferBit)

            Dim PerspectiveMat As Matrix4
            'Dim lookat As Matrix4
            If ProjectionType = "ORTHOGRAPHIC" Then
                PerspectiveMat = Matrix4.CreateOrthographicOffCenter(0, ScreenWidth, ScreenHeight, 0, -120.0F, 120.0F) 'Matrix4.CreatePerspectiveFieldOfView(1.04, 4 / 3, 1, 10000) 'Setup Perspective
            ElseIf ProjectionType = "PERSPECTIVE" Then
                PerspectiveMat = Matrix4.CreatePerspectiveFieldOfView(FOV, 4 / 3, 1, 1200) 'Setup Perspective
            End If

            GL.MatrixMode(MatrixMode.Projection)
            GL.LoadIdentity()
            GL.LoadMatrix(PerspectiveMat)
            GL.MatrixMode(MatrixMode.Modelview)
            GL.LoadIdentity()
            GL.Viewport(0, 0, ScreenWidth, ScreenHeight) 'Window size

            'Rotating/Translating, we'll look into that later
            GL.LoadIdentity()
            GL.Rotate(Pitch, 1.0F, 0.0F, 0.0F)
            GL.Rotate(Yaw, 0.0F, 1.0F, 0.0F)
            GL.Rotate(Roll, 0.0F, 0.0F, 1.0F)
            GL.Translate(TranslateX, TranslateY, TranslateZ)

            RenderF("DRAW")
        ElseIf Func = "DRAW" Then
            'HERE WE'LL ADD ALL OUR DRAWING UNTIL WE SWITCH FROM IMMEDIATE DRAWING
        End If
    End Sub
End Module

Which is pretty much what we used before, on the introduction to OpenGL post.

If you add the code above you should run your project and get a black window that closes on key pressing.

If you see when the game window class is created this MyBase.New(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, New OpenTK.Graphics.GraphicsMode(32, 8, 0, 0)) gets your screen resolution and creates the window.

        Dim SPoint As Point = New Point(0, 0) 'Create a new point 0,0
        MyBase.Location = SPoint 'Move the window to 0,0 point
        'MyBase.WindowBorder = OpenTK.WindowBorder.Hidden 'No border on the window
        'MyBase.WindowState = OpenTK.WindowState.Fullscreen 'Full screen mode
        MyBase.VSync = True 'VSync enabled
        'MyBase.WindowState = OpenTK.WindowState.Maximized 'Maximized state

We'll need a UI sprite sheet, and a font sheet which we'll create in a next post.

I will write the same code in the console application and whenever I need something fast I'll check it in the windows form application for easiness.

 

Comments