3H-GDC m.V: Something Special (winner: BeanDog)

Started by
213 comments, last by capn_midnight 18 years, 1 month ago
Well, I spent an hour and made a neat intro with MDX playing a few MIDIs in C#.

The performance isn't too great though (About 10 FPS). And I haven't a C# way to get keyboard input.

My Code:
using System;using System.Collections.Generic;using System.Text;using System.Media;using System.Drawing;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.AudioVideoPlayback;public partial class cMain{    public class ASprite    {        public Color MyTransparentConst = Color.FromArgb(255, 0, 255);        public int TheWidth;        public int TheHeight;        public Color[] TheColors;        public ASprite(int TheWidth, int TheHeight)        {            this.TheWidth = TheWidth;            this.TheHeight = TheHeight;            TheColors = new Color[this.TheWidth * this.TheHeight];            for (int TheLoop = 0; TheLoop < this.TheWidth * this.TheHeight; TheLoop++)                TheColors[TheLoop] = new Color();        }        public ASprite(string TheFileName)        {            Bitmap TheBitmap = new Bitmap(TheFileName);            this.TheWidth = TheBitmap.Width;            this.TheHeight = TheBitmap.Height;            TheColors = new Color[this.TheWidth * this.TheHeight];            for (int TheLoop = 0; TheLoop < this.TheWidth * this.TheHeight; TheLoop++)                TheColors[TheLoop] = new Color();            for (int TheX = 0; TheX < this.TheWidth; TheX++)                for (int TheY = 0; TheY < this.TheHeight; TheY++)                    TheColors[TheX + TheY * this.TheWidth] = TheBitmap.GetPixel(TheX, TheY);        }        public void sDraw(int TheSrcX, int TheSrcY, int TheSrcWidth, int TheSrcHeight,                          int TheDestX, int TheDestY, Color[] TheDestination)        {            for (int TheX = 0; TheX < TheSrcWidth; TheX++)                for (int TheY = 0; TheY < TheSrcHeight; TheY++)                    /* Lets just make sure we are within bounds. */                    if (TheX >= 0 && TheX < 80 &&                       TheY >= 0 && TheY < 50)                        /* And lets make sure its not a transparency color. */                        if(this.TheColors[TheSrcX+TheX + (TheSrcY+TheY)*TheWidth] != MyTransparentConst)                            TheDestination[TheX + TheDestX + (TheY + TheDestY) * 80] =                                this.TheColors[TheSrcX + TheX + (TheSrcY + TheY) * TheWidth];        }    }    /* Load the music files needed. */    public Audio MyIntroMusic = new Audio("Resources\\Intro.mid");    /* Load the sources needed. */    public ASprite MyLogo = new ASprite("Resources\\Logo.png");    /* Game variables. */    public enum EGameState    {        Menu,        InGame,        Quit    }    public EGameState MyGameState = EGameState.Menu;    /* My render framebuffer. */    public Color[] MyRender = null;    public Color[] MyPreviousRender = null;    /* Various Animations. */    public int TheLogoAnimation = 0;    /* No touch. */    static void Main(string[] args)    {        cMain TheGame = new cMain();        TheGame.sLoad();    }    /* Load procedure; also main game loop. */    public void sLoad()    {        /* Configure the console window. */        Console.WindowWidth = 80;        Console.WindowHeight = 50;        Console.Title = "GameDev.Net 3H-GDC (Thevenin Entry)";        /* Build my renderer. */        MyRender = new Color[80*50];        for(int TheLoop=0; TheLoop<80*50; TheLoop++)            MyRender[TheLoop] = new Color();        MyPreviousRender = new Color[80 * 50];        for (int TheLoop = 0; TheLoop < 80 * 50; TheLoop++)            MyPreviousRender[TheLoop] = new Color();        /* Get the ticks. */        int TheCurrentTickCount = Environment.TickCount;        int TheLastTickCount = TheCurrentTickCount;        /* Start the music. */        MyIntroMusic.Play();        /* Game loop. */        while(MyGameState == EGameState.InGame ||               MyGameState == EGameState.Menu)        {            TheCurrentTickCount = Environment.TickCount;            if(TheCurrentTickCount - TheLastTickCount >= 100)            {                /* Get the graphics thingy ready. */                sPrepareRender();                TheLastTickCount = TheCurrentTickCount;            }        }        /* Finish. */        MessageBox.Show("Thank you for playing~");    }    /* Prepare the Render. */    public void sPrepareRender()    {        TheLogoAnimation++;        if (TheLogoAnimation == 10)        {            MyLogo.sDraw(0, 0, 80, 50, 0, 0, MyRender);            sRender();        }        else if (TheLogoAnimation == 20)        {            MyLogo.sDraw(0, 50, 80, 50, 0, 0, MyRender);            sRender();            TheLogoAnimation = 0;        }    }    /* Render a frame. */    public void sRender()    {        for (int TheY = 0; TheY < 50; TheY++)            for(int TheX = 0; TheX < 80; TheX++)                if(TheY != 49 || TheX != 79)                    if(MyRender[TheX+TheY*80] != MyPreviousRender[TheX+TheY*80])                    {                        Color TheColor = MyRender[TheX + TheY*80];                        if (TheColor == Color.FromArgb(0,0,255))                            Console.BackgroundColor = ConsoleColor.Blue;                        else if (TheColor == Color.FromArgb(0, 0, 128))                            Console.BackgroundColor = ConsoleColor.DarkBlue;                        else if (TheColor == Color.FromArgb(0, 0, 0))                            Console.BackgroundColor = ConsoleColor.Black;                        else if (TheColor == Color.FromArgb(255, 255, 255))                            Console.BackgroundColor = ConsoleColor.White;                        else if (TheColor == Color.FromArgb(0, 255, 0))                            Console.BackgroundColor = ConsoleColor.Green;                        else if (TheColor == Color.FromArgb(0, 128, 0))                            Console.BackgroundColor = ConsoleColor.DarkGreen;                        Console.SetCursorPosition(TheX, TheY);                        Console.Write(" ");// added in as an optimization instead of putting it below in the commeneted out region.MyPreviousRender[TheX + TheY * 80] = MyRender[TheX + TheY * 80];                    }        /* Lastly, we'll want to copy the new render onto the old one. */        //for (int TheY = 0; TheY < 50; TheY++)        //    for (int TheX = 0; TheX < 80; TheX++)        //        MyPreviousRender[TheX + TheY * 80] = MyRender[TheX + TheY * 80];    }}


I might just use WindowsForms, since I don't want to attempt this in 3 hours in Procedural-C...

[Edited by - Thevenin on February 8, 2006 9:45:49 PM]
Advertisement

d000hg's D3D Console Emulator v1.0


I thought I'd make all the base code I plan to use available to you all, in case anyone thought of entering but couldn't be arsed to find out how to do text-mode coding.
It uses D3D8.1 and supports the following:
  • Set character, with foreground & background colour.

  • Set character without changing colours at that location

  • Set colours without changing the character at that location

  • Draw dithered blocks

  • Draw dithered rectangles

  • Draw a frame/border, or a dual-panel frame (see screenshot)

  • Draw a string, which wraps around if required


  • You can get it here:
    http://www.sendmefile.com/00274115, including the .dds font file and a VS6 workspace. A sample app is present including D3D initialisation. Check D3DConsoleEmulator.h for a list of special characters like those in the screenshot.
    You may use any of this code for any purpose as long as you let me know and give me a mention somewhere.
    It's not meant to be especially good, it's just thrown together for fun! But suggest improvements or report bugs and I'll address them.
    If I make any fixes or add new stuff before Saturday I'll try to release the updated version.
    I threw in a few comments and it should be fairly self explanatory especially from the sample app but feel free to ask me any questions.
    Quote:Original post by paulecoyote
    another question - anyone know of any good .Net 2 console input examples suitable for controlling a game?

    Trying to get my head around DllImporting GetKeyState and GetKeyboardState and don't seem to be getting very far.

    It's odd cos I could do this keyboard control in C++ no troubles, but I kind of want to do this in C# 2 [smile]


    Here is the .NET 2.0 Class Library Reference
    here is the System.Console class
    bool System.Console.KeyAvailable is a boolean property that returns true if a key is on the input stream
    System.ConsoleKeyInfo System.Console.ReadKey([bool suppresDisplay]) blocks and returns a ConsoleKeyInfo struct when a key is available on the input stream. It also has an optional boolean parameter that setting to true causes the key to not echo to the screen
    System.ConsoleKeyInfo primarily holds flags for the ctrl, shift, and alt key, as well as a value for the System.ConsoleKey that was pressed
    System.ConsoleKey is an enumeration of virtual keys, including definitions for the arrow keys.

    [Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

    Quote:Original post by capn_midnight
    Quote:Original post by paulecoyote
    another question - anyone know of any good .Net 2 console input examples suitable for controlling a game?

    Trying to get my head around DllImporting GetKeyState and GetKeyboardState and don't seem to be getting very far.

    It's odd cos I could do this keyboard control in C++ no troubles, but I kind of want to do this in C# 2 [smile]


    Here is the .NET 2.0 Class Library Reference
    here is the System.Console class
    bool System.Console.KeyAvailable is a boolean property that returns true if a key is on the input stream
    System.ConsoleKeyInfo System.Console.ReadKey([bool suppresDisplay]) blocks and returns a ConsoleKeyInfo struct when a key is available on the input stream. It also has an optional boolean parameter that setting to true causes the key to not echo to the screen
    System.ConsoleKeyInfo primarily holds flags for the ctrl, shift, and alt key, as well as a value for the System.ConsoleKey that was pressed
    System.ConsoleKey is an enumeration of virtual keys, including definitions for the arrow keys.


    Hey thanks for that. I did see some of that stuff but I was looking to figure out keydown and keyup events. In the end I managed to wrap win32 ReadConsoleInput and some over pinvoked functions to do that. I expect I'll post the code with my entry - I want to comment it some more and hammer it before inflicting it on the world. I'm determined to get an entry in for one of these competitions!

    So can I use my friends music with his permission then?
    Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
    Yeah, you can use it. I'll consider you as a team. *only one copy of prizes per team*

    [Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

    Quote:Original post by capn_midnight
    Yeah, you can use it. I'll consider you as a team. *only one copy of prizes per team*


    awesome.
    Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
    Is 80x50 or 80x43 allowed?
    Quote:Original post by matt_j
    Is 80x50 or 80x43 allowed?

    This question has already been fielded. I believe the answers was "yeeeeup".
    Quote:Original post by matt_j
    Is 80x50 or 80x43 allowed?


    They are both allowed. Be warned though, as the screensize increases, the framerate descreases.

    Us C# Console coders (Without using the Win32API) have some solid restraints on what we can do due the bad performance....

    Here's a quicky on what I'm working on (it glows! [grin]). Less than 10 FPS though, and the only redraws are done on parts that have changed.
    Cool. Are the glow effects actually a special effect, or does the original 'artwork' include those glows in the first place?

    This topic is closed to new replies.

    Advertisement