Fix transparency errors, add draw depth
Also a demonstration of an early attempt at background drawing. Will make a separate class for BGs later.
This commit is contained in:
parent
581a22dd56
commit
77b99d3143
BIN
JustTheBasics/JustTheBasics/CircuitBOTTOM.png
Normal file
BIN
JustTheBasics/JustTheBasics/CircuitBOTTOM.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 180 KiB |
@ -76,6 +76,9 @@
|
|||||||
<Content Include="Bricks.png">
|
<Content Include="Bricks.png">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
<Content Include="CircuitBOTTOM.png">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</Content>
|
||||||
<Content Include="LifeIcon.png">
|
<Content Include="LifeIcon.png">
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
</Content>
|
</Content>
|
||||||
|
@ -9,6 +9,7 @@ using System.Drawing;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
using OpenTK.Input;
|
using OpenTK.Input;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace JustTheBasics
|
namespace JustTheBasics
|
||||||
{
|
{
|
||||||
@ -38,7 +39,10 @@ namespace JustTheBasics
|
|||||||
Vector2[] texcoorddata;
|
Vector2[] texcoorddata;
|
||||||
|
|
||||||
// List of all the sprites we will be rendering
|
// List of all the sprites we will be rendering
|
||||||
List<GameObject> objects = new List<GameObject>();
|
//List<GameObject> objects = new List<GameObject>();
|
||||||
|
|
||||||
|
// Dictionary of Lists of GameObjects to specify relative draw order (i.e. depth)
|
||||||
|
Dictionary<int, List<GameObject>> objects = new Dictionary<int, List<GameObject>>();
|
||||||
|
|
||||||
// Dictionary to store texture ID's by name
|
// Dictionary to store texture ID's by name
|
||||||
Dictionary<string, int> textures = new Dictionary<string, int>();
|
Dictionary<string, int> textures = new Dictionary<string, int>();
|
||||||
@ -63,6 +67,11 @@ namespace JustTheBasics
|
|||||||
// Declare that the shader we will use first is the textured sprite
|
// Declare that the shader we will use first is the textured sprite
|
||||||
activeShader = "textured";
|
activeShader = "textured";
|
||||||
|
|
||||||
|
// Initialize the lists of objects in the dictionary
|
||||||
|
objects.Add(1, new List<GameObject>());
|
||||||
|
objects.Add(2, new List<GameObject>());
|
||||||
|
objects.Add(64, new List<GameObject>());
|
||||||
|
|
||||||
// Create a new sprite here
|
// Create a new sprite here
|
||||||
// In final compiled game, should actually make gameObjects here and create the sprites
|
// In final compiled game, should actually make gameObjects here and create the sprites
|
||||||
// inside those. For now, Sprite and GameObject will be synonymous.
|
// inside those. For now, Sprite and GameObject will be synonymous.
|
||||||
@ -76,10 +85,10 @@ namespace JustTheBasics
|
|||||||
Vector2[] spawn = new Vector2[] { new Vector2(16, 16), new Vector2(16, 32), new Vector2(32, 32), new Vector2(32, 16), new Vector2(16, 16) };
|
Vector2[] spawn = new Vector2[] { new Vector2(16, 16), new Vector2(16, 32), new Vector2(32, 32), new Vector2(32, 16), new Vector2(16, 16) };
|
||||||
|
|
||||||
float[] map = new float[] { 0f, 0f, Width, Height };
|
float[] map = new float[] { 0f, 0f, Width, Height };
|
||||||
GameObject objPlayer = new GameObject("Player", spawn, map, 2.0f, 0.0f, true, tc);
|
GameObject objPlayer = new GameObject("Player", spawn, map, 5.0f, 0.0f, true, tc);
|
||||||
|
|
||||||
// Add the sprite to our list of active Sprites
|
// Add the sprite to our list of active Sprites
|
||||||
objects.Add(objPlayer);
|
objects[1].Add(objPlayer);
|
||||||
|
|
||||||
//--------------- Create bricks ------------------------------
|
//--------------- Create bricks ------------------------------
|
||||||
// Create a new sprite here
|
// Create a new sprite here
|
||||||
@ -97,7 +106,16 @@ namespace JustTheBasics
|
|||||||
GameObject objBricks = new GameObject("Bricks", spawnBrick, map, 0.0f, 0.0f, true, bc);
|
GameObject objBricks = new GameObject("Bricks", spawnBrick, map, 0.0f, 0.0f, true, bc);
|
||||||
|
|
||||||
// Add the sprite to our list of active Sprites
|
// Add the sprite to our list of active Sprites
|
||||||
objects.Add(objBricks);
|
objects[2].Add(objBricks);
|
||||||
|
|
||||||
|
// Create a background
|
||||||
|
Sprite bg = new Sprite();
|
||||||
|
textures.Add("circuit.png", loadImage("CircuitBOTTOM.png", bg));
|
||||||
|
bg.TextureID = textures["circuit.png"];
|
||||||
|
|
||||||
|
Vector2[] spawnBG = new Vector2[] { new Vector2(80, 0), new Vector2(80, 640), new Vector2(720, 640), new Vector2(720, 0), new Vector2(80, 0) };
|
||||||
|
GameObject BG = new GameObject("BG", spawnBG, map, 0.0f, 0.0f, false, bg);
|
||||||
|
objects[64].Add(BG);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function overrides the base OnLoad function
|
// This function overrides the base OnLoad function
|
||||||
@ -113,7 +131,7 @@ namespace JustTheBasics
|
|||||||
|
|
||||||
Title = "Hello OpenTK!";
|
Title = "Hello OpenTK!";
|
||||||
|
|
||||||
GL.ClearColor(Color.CornflowerBlue); // Yech, but at least it makes it easy to see mistakes.
|
GL.ClearColor(Color.Black); // Yech, but at least it makes it easy to see mistakes.
|
||||||
GL.PointSize(5f);
|
GL.PointSize(5f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,51 +156,66 @@ namespace JustTheBasics
|
|||||||
// Total number of processed vertices
|
// Total number of processed vertices
|
||||||
int vertcount = 0;
|
int vertcount = 0;
|
||||||
|
|
||||||
// Loop over every sprite in the game (later should be GameObject)
|
// Stack for LIFO behavior in drawing sprites
|
||||||
foreach (GameObject go in objects)
|
Stack<GameObject> objLists = new Stack<GameObject>();
|
||||||
|
|
||||||
|
// Loop over every GameObject in the game
|
||||||
|
// First put the highest-draw layer objects at the bottom so they draw last
|
||||||
|
foreach (int i in objects.Keys)
|
||||||
{
|
{
|
||||||
Sprite v = go.sprite;
|
foreach (GameObject o in objects[i])
|
||||||
/*
|
|
||||||
//get state of all keyboards on device
|
|
||||||
var state = OpenTK.Input.Keyboard.GetState();
|
|
||||||
//checks up key, if it is pressed it will update location.
|
|
||||||
if (state[Key.Up])
|
|
||||||
{
|
{
|
||||||
// Set the sprite's position
|
objLists.Push(o);
|
||||||
v.Position.Y += 5f / Height;
|
|
||||||
}
|
}
|
||||||
if (state[Key.Down])
|
}
|
||||||
|
// Loop over each list in the stack and draw them
|
||||||
|
while (objLists.Count > 0)
|
||||||
|
{
|
||||||
|
GameObject go = objLists.Pop();
|
||||||
{
|
{
|
||||||
v.Position.Y -= 5f / Height;
|
Sprite v = go.sprite;
|
||||||
}
|
/*
|
||||||
if (state[Key.Right])
|
//get state of all keyboards on device
|
||||||
{
|
var state = OpenTK.Input.Keyboard.GetState();
|
||||||
v.Position.X += 5f / Width;
|
//checks up key, if it is pressed it will update location.
|
||||||
}
|
if (state[Key.Up])
|
||||||
if (state[Key.Left])
|
{
|
||||||
{
|
// Set the sprite's position
|
||||||
v.Position.X -= 5f / Width;
|
v.Position.Y += 5f / Height;
|
||||||
}
|
}
|
||||||
*/
|
if (state[Key.Down])
|
||||||
go.Update();
|
{
|
||||||
float saveX = v.Position.X;
|
v.Position.Y -= 5f / Height;
|
||||||
float saveY = v.Position.Y;
|
}
|
||||||
|
if (state[Key.Right])
|
||||||
|
{
|
||||||
|
v.Position.X += 5f / Width;
|
||||||
|
}
|
||||||
|
if (state[Key.Left])
|
||||||
|
{
|
||||||
|
v.Position.X -= 5f / Width;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
go.Update();
|
||||||
|
float saveX = v.Position.X;
|
||||||
|
float saveY = v.Position.Y;
|
||||||
|
|
||||||
v.Position.X = v.Position.X / Width;
|
v.Position.X = v.Position.X / Width;
|
||||||
v.Position.Y = v.Position.Y / Height;
|
v.Position.Y = v.Position.Y / Height;
|
||||||
|
|
||||||
// Populate the previously defined lists
|
// Populate the previously defined lists
|
||||||
verts.AddRange(v.GetVerts(Width, Height).ToList());
|
verts.AddRange(v.GetVerts(Width, Height).ToList());
|
||||||
inds.AddRange(v.GetIndices(vertcount).ToList());
|
inds.AddRange(v.GetIndices(vertcount).ToList());
|
||||||
colors.AddRange(v.GetColorData().ToList());
|
colors.AddRange(v.GetColorData().ToList());
|
||||||
vertcount += v.VertCount;
|
vertcount += v.VertCount;
|
||||||
texcoords.AddRange(v.GetTextureCoords());
|
texcoords.AddRange(v.GetTextureCoords());
|
||||||
|
|
||||||
// Update the matrix used to calculate the Sprite's visuals
|
// Update the matrix used to calculate the Sprite's visuals
|
||||||
v.CalculateModelMatrix();
|
v.CalculateModelMatrix();
|
||||||
// Offset it by our viewport matrix (for things like scrolling levels)
|
// Offset it by our viewport matrix (for things like scrolling levels)
|
||||||
v.ModelViewProjectionMatrix = v.ModelMatrix;// * ortho;
|
v.ModelViewProjectionMatrix = v.ModelMatrix;// * ortho;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the lists into easier to use arrays
|
// Convert the lists into easier to use arrays
|
||||||
@ -233,7 +266,7 @@ namespace JustTheBasics
|
|||||||
// Clear the graphics drawn last frame to avoid weird effects.
|
// Clear the graphics drawn last frame to avoid weird effects.
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||||
// Enable several important switches to be able to draw flat images and make a generally pretty picture.
|
// Enable several important switches to be able to draw flat images and make a generally pretty picture.
|
||||||
GL.Enable(EnableCap.DepthTest);
|
//GL.Enable(EnableCap.DepthTest);
|
||||||
GL.Enable(EnableCap.CullFace);
|
GL.Enable(EnableCap.CullFace);
|
||||||
GL.Enable(EnableCap.Blend);
|
GL.Enable(EnableCap.Blend);
|
||||||
GL.Enable(EnableCap.Texture2D);
|
GL.Enable(EnableCap.Texture2D);
|
||||||
@ -246,31 +279,48 @@ namespace JustTheBasics
|
|||||||
// Index counter, since we turned some lists into arrays and need to offset accordingly
|
// Index counter, since we turned some lists into arrays and need to offset accordingly
|
||||||
int indiceat = 0;
|
int indiceat = 0;
|
||||||
|
|
||||||
// Again, this should be GameObjects later.
|
// Loop over every GameObject in the game
|
||||||
foreach (GameObject go in objects)
|
// First put the highest-draw layer objects at the bottom so they draw last
|
||||||
|
// Stack for LIFO behavior in drawing sprites
|
||||||
|
Stack<GameObject> objLists = new Stack<GameObject>();
|
||||||
|
|
||||||
|
// Loop over every GameObject in the game
|
||||||
|
// First put the highest-draw layer objects at the bottom so they draw last
|
||||||
|
foreach (int i in objects.Keys)
|
||||||
{
|
{
|
||||||
Sprite v = go.sprite;
|
foreach (GameObject o in objects[i])
|
||||||
|
|
||||||
// Tell OpenTK to associate the given texture to the VBO we're drawing
|
|
||||||
GL.BindTexture(TextureTarget.Texture2D, v.TextureID);
|
|
||||||
// Send our projection matrix to the GLSL shader
|
|
||||||
GL.UniformMatrix4(shaders[activeShader].GetUniform("modelview"), false, ref v.ModelViewProjectionMatrix);
|
|
||||||
|
|
||||||
// If shader uses textures, send the image to the shader code for processing
|
|
||||||
if (shaders[activeShader].GetAttribute("maintexture") != -1)
|
|
||||||
{
|
{
|
||||||
GL.Uniform1(shaders[activeShader].GetAttribute("maintexture"), v.TextureID);
|
objLists.Push(o);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// Loop over each list in the stack and draw them
|
||||||
|
while (objLists.Count > 0)
|
||||||
|
{
|
||||||
|
GameObject go = objLists.Pop();
|
||||||
|
{
|
||||||
|
Sprite v = go.sprite;
|
||||||
|
|
||||||
// Draw a square/rectangle
|
// Tell OpenTK to associate the given texture to the VBO we're drawing
|
||||||
GL.DrawElements(BeginMode.Quads, v.IndiceCount, DrawElementsType.UnsignedInt, indiceat * sizeof(uint));
|
GL.BindTexture(TextureTarget.Texture2D, v.TextureID);
|
||||||
// Increment our index counter by the number of indices processed
|
// Send our projection matrix to the GLSL shader
|
||||||
indiceat += v.IndiceCount;
|
GL.UniformMatrix4(shaders[activeShader].GetUniform("modelview"), false, ref v.ModelViewProjectionMatrix);
|
||||||
|
|
||||||
|
// If shader uses textures, send the image to the shader code for processing
|
||||||
|
if (shaders[activeShader].GetAttribute("maintexture") != -1)
|
||||||
|
{
|
||||||
|
GL.Uniform1(shaders[activeShader].GetAttribute("maintexture"), v.TextureID);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw a square/rectangle
|
||||||
|
GL.DrawElements(BeginMode.Quads, v.IndiceCount, DrawElementsType.UnsignedInt, indiceat * sizeof(uint));
|
||||||
|
// Increment our index counter by the number of indices processed
|
||||||
|
indiceat += v.IndiceCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free up the memory off the GPU
|
// Free up the memory off the GPU
|
||||||
shaders[activeShader].DisableVertexAttribArrays();
|
shaders[activeShader].DisableVertexAttribArrays();
|
||||||
|
|
||||||
// Draw the final buffer (or canvas) to screen
|
// Draw the final buffer (or canvas) to screen
|
||||||
GL.Flush();
|
GL.Flush();
|
||||||
SwapBuffers();
|
SwapBuffers();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user