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:
Chris N 2016-04-01 15:31:32 -10:00
parent 581a22dd56
commit 77b99d3143
3 changed files with 114 additions and 61 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

View File

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

View File

@ -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,8 +156,22 @@ 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)
{
foreach (GameObject o in objects[i])
{
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; Sprite v = go.sprite;
/* /*
@ -184,6 +216,7 @@ namespace JustTheBasics
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
vertdata = verts.ToArray(); vertdata = verts.ToArray();
@ -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,8 +279,24 @@ 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)
{
foreach (GameObject o in objects[i])
{
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; Sprite v = go.sprite;
@ -267,6 +316,7 @@ namespace JustTheBasics
// Increment our index counter by the number of indices processed // Increment our index counter by the number of indices processed
indiceat += v.IndiceCount; indiceat += v.IndiceCount;
} }
}
// Free up the memory off the GPU // Free up the memory off the GPU
shaders[activeShader].DisableVertexAttribArrays(); shaders[activeShader].DisableVertexAttribArrays();