Finally on the verge to decide whether C# or Visual Basic?-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 48

Thread: Finally on the verge to decide whether C# or Visual Basic?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    2

    Question Finally on the verge to decide whether C# or Visual Basic?

    Hello there everyone.
    I needed the suggestions about whether to pick C# or Visual Basic 2010 to learn?

    Haven't Programmed Before at all..

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,573

    Re: Finally on the verge to decide whether C# or Visual Basic?

    I think, VB will be easy for you. Later you could port to C# if needed. I mean, after getting the basic programming concepts.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India

    Skills: PHP, MySQL, jQuery, VB.Net, VB6, Photoshop...

  3. #3
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,892

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Yes, VB would be easier if you are coming from VB6.

    Edit:

    Just realized you are new to programming but I think what I said still applies.
    Last edited by Nightwalker83; Feb 28th, 2012 at 02:38 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Is there really a difference? They're practically the same language with slightly different syntax.

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by cicatrix View Post
    Is there really a difference? They're practically the same language with slightly different syntax.
    I would actually not say that the syntax is slightly different. The syntax is (almost very!) different. Semantics, thats another story...
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    What? The type name declaration instead of name As type and {} instead of Sub/Function...End Sub/Function... oh, almost forgot - == instead of = for comparison and != instead of <>... what did I miss?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Finally on the verge to decide whether C# or Visual Basic?

    But what is syntax if not those things you just mentioned?
    There are more differences than what you mention, raising and handling events is one of the things that come to mind.
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    That's quite boring, actually. My sarcasm gone unnoticed. Well, yes. THERE ARE differences in syntax, but for the most part a program either in VB or C# consists of .Net Framework calls anyway. Those differences you talk about can be memorized in about 30 minutes and take about a hour to get used to. When I switch from c# to vb and back it takes me about that much to make a 'mental switch' so I really don't see any differences at all. In many ways C# differences from its older brother C++ are much more profound than from VB.Net even though the syntax is practically the same.

  9. #9
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,573

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by cicatrix View Post
    That's quite boring, actually. My sarcasm gone unnoticed. Well, yes. THERE ARE differences in syntax, but for the most part a program either in VB or C# consists of .Net Framework calls anyway. Those differences you talk about can be memorized in about 30 minutes and take about a hour to get used to. When I switch from c# to vb and back it takes me about that much to make a 'mental switch' so I really don't see any differences at all. In many ways C# differences from its older brother C++ are much more profound than from VB.Net even though the syntax is practically the same.
    The OP is a complete newbie.

    Quote Originally Posted by love.to.code View Post
    Hello there everyone.
    I needed the suggestions about whether to pick C# or Visual Basic 2010 to learn?

    Haven't Programmed Before at all..

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India

    Skills: PHP, MySQL, jQuery, VB.Net, VB6, Photoshop...

  10. #10
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by akhileshbc View Post
    The OP is a complete newbie.
    That's an advantage, not a drawback. OP can learn both vb.net and c# with almost equal efficiency.

  11. #11
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,054

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Just say no to ;.
    My usual boring signature: Nothing

  12. #12

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    11,318

    Re: Finally on the verge to decide whether C# or Visual Basic?

    .Net has a Basic for you:

    http://smallbasic.com/

  14. #14
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,054

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by cicatrix View Post
    != vs <> is quite frustrating, by the way
    That actually points out the subtle problem with C style languages in general: They make excessive use of very hard to type operator combinations. The semicolon is one of the easiest (although it is totally unnecessary and does nothing but cause bugs anymore), but != is VASTLY harder than <>, as the former stretches both of your hands. The {} in C languages has the same issue, as do most of the other operators. Of course, most of the operators are used in VB, too, including >>,<<,+,-,*,&,^,\,/,(, and ). VB doesn't use the !, though, nor the |, and not much of the ? or :, and it uses much less of the * and &.

    VB is a better typist language.
    My usual boring signature: Nothing

  15. #15
    Member
    Join Date
    Mar 2011
    Posts
    54

    Re: Finally on the verge to decide whether C# or Visual Basic?

    1st. c++
    2nd. vba(optional if your not into MS applications ie, MS Excel)
    3rd. vb6
    4th. c#

    better off start on basic languages with basic tools rather than equip yourself with newer advance tools than cant be seen on older languages, that way you can adjust more. then off you go to new languages. good luck!
    Last edited by lehgzil; Feb 28th, 2012 at 07:52 PM.
    ~liz
    ('@ ' )~

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,810

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by Nightwalker83 View Post
    Yes, VB would be easier if you are coming from VB6.
    I am one of those that finds it easier to program in C# rather than in VB.Net when coming from VB6, VB.Net and VB6 has almost the same syntax than I tend to mix them and that is avoided when I am using C#.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by Shaggy Hiker View Post
    That actually points out the subtle problem with C style languages in general: They make excessive use of very hard to type operator combinations. The semicolon is one of the easiest (although it is totally unnecessary and does nothing but cause bugs anymore), but != is VASTLY harder than <>, as the former stretches both of your hands. The {} in C languages has the same issue, as do most of the other operators. Of course, most of the operators are used in VB, too, including >>,<<,+,-,*,&,^,\,/,(, and ). VB doesn't use the !, though, nor the |, and not much of the ? or :, and it uses much less of the * and &.

    VB is a better typist language.
    I think I disagree with everything you've said in this post. Well done.

  18. #18
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by lehgzil View Post
    1st. c++
    2nd. vba(optional if your not into MS applications ie, MS Excel)
    3rd. vb6
    4th. c#

    better off start on basic languages with basic tools rather than equip yourself with newer advance tools than cant be seen on older languages, that way you can adjust more. then off you go to new languages. good luck!
    C++ is not a basic tool. Or, following your logic, it's better to start with the assembly language as a basic tool. Well, yes, it would probably be better from educational point of view, but throwing a newbie on C++ or assembly is almost a guaranteed way to force him to give the whole thing up.

    Why do we need vb6? It can only get you a wrong perception of OOP, nothing more.

  19. #19
    Member
    Join Date
    Mar 2011
    Posts
    54

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by cicatrix View Post
    C++ is not a basic tool. Or, following your logic, it's better to start with the assembly language as a basic tool.
    sorry i haven't cleared it out. what i mean of basic languages isn't "BASIC" language but of those programming languages that have easier to understand syntax and functionality.

    Quote Originally Posted by cicatrix View Post
    but throwing a newbie on C++ or assembly is almost a guaranteed way to force him to give the whole thing up.
    C++ is just to start him, never meant he will stay there or make his career on it or it depends on him, but i must say, he can go straight where he is comfortable, that way he can branch out and progress to other languages he can adapt easier.

    To think of it C++ is just on my list, and never on OP`s post
    ~liz
    ('@ ' )~

  20. #20
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Why not tell him the truth?

    You will use VBA and Delphi and C# and Java and COBOL and Ada and ..., that will solve various tasks.

  21. #21
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by klen_ View Post
    Why not tell him the truth?

    You will use VBA and Delphi and C# and Java and COBOL and Ada and ..., that will solve various tasks.
    lmao or simply use Emacs.

  22. #22
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    3,326

    Re: Finally on the verge to decide whether C# or Visual Basic?

    That actually points out the subtle problem with C style languages in general: They make excessive use of very hard to type operator combinations. The semicolon is one of the easiest (although it is totally unnecessary and does nothing but cause bugs anymore), but != is VASTLY harder than <>, as the former stretches both of your hands. The {} in C languages has the same issue, as do most of the other operators. Of course, most of the operators are used in VB, too, including >>,<<,+,-,*,&,^,\,/,(, and ). VB doesn't use the !, though, nor the |, and not much of the ? or :, and it uses much less of the * and &.

    VB is a better typist language.
    That's a really interesting point of view. The justification I've always heard for C++'s terse syntax was that it was an engineers language and they wanted to reduce typing. Certainly :: takes a lot less time to type than Inherits, for example. The more I think about it, though, number of characters doesn't really matter in a world of intelli-sense so the position of characters on the keyboard is more important now... and I think I agree with you that C#'s character set is more annoyingly spaced out and, although I'm comfortable programming in both, I'm fairly sure my fingers move faster in VB.

    And speaking of terse...
    I think I disagree with everything you've said in this post. Well done.
    That actually made me laugh out loud. Well done.


    1st. c++
    2nd. vba(optional if your not into MS applications ie, MS Excel)
    3rd. vb6
    4th. c#
    Really?! What have you been smoking and where can I get some? You can't really suggest C++ as a beginners language. The amount of syntax one has to learn for C++ dwarves the amount for VB.Net shich practically spoon feeds you its syntax. If the learner wants to do anything more complex than a command line version of hello world I'd steer well clear of it. And why would you even touch VBA unless your doing specifically office development. And why, at this point, would possibly look at VB6? I'm not sure it's dead but it's definitely living in a retirement home and taking weekend coah trips to Worthing beach.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush

  23. #23
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    VBA has its uses still even if you don't do office development. My primary 'working tool' at the moment is MS Outlook and quite often I simply launch a built-in VB Editor instead of launching a VS IDE.

  24. #24
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,054

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by penagate View Post
    I think I disagree with everything you've said in this post. Well done.
    I don't see on what basis you could disagree with that. Those key combinations are all harder to hit than the VB alternative.
    My usual boring signature: Nothing

  25. #25
    PowerPoster
    Join Date
    May 2002
    Posts
    25,297

    Re: Finally on the verge to decide whether C# or Visual Basic?

    So... you ARE doing Office Development then... so how is VBA useful even if you aren't doing office development?

    That aside... 1) the OP is new to development.
    2) He's already narrowed it down to VB.NET and C#... so discussions around portability and VBA and VB6 are not wholly relevant to the discussion.
    3) I don't exactly consider C++ a beginner's language...
    4) It might be helpful for people to read this thread - http://www.vbforums.com/showthread.php?t=673501 - might provide some context on where the OP is coming from.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  26. #26
    Member
    Join Date
    Mar 2011
    Posts
    54

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by FunkyDexter View Post
    Really?! What have you been smoking and where can I get some? You can't really suggest C++ as a beginners language.
    complex methods and ideas can be picked up on C++, which in turn may fuel your urge to learn things, and that you may value core programming.

    Quote Originally Posted by FunkyDexter View Post
    The amount of syntax one has to learn for C++ dwarves the amount for VB.Net shich practically spoon feeds you its syntax. If the learner wants to do anything more complex than a command line version of hello world I'd steer well clear of it.
    in c++

    #include <stdio.h>

    int main()
    {
    printf("Hello World");
    }

    vb

    Private Sub Form_Load()
    MsgBox ("hello world")
    End Sub

    You said it right,vb-vb.net may spoon feed all we/you/his need, then again going from a command line language may lead you to appreciate this "spoon feeding".

    we can steer away from my argument as it is only on my perception. i am sorry if i have caused another path from the topic.
    ~liz
    ('@ ' )~

  27. #27
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by Shaggy Hiker View Post
    I don't see on what basis you could disagree with that.
    Reasons why I disagree:

    I don't find it harder to type in a C-style grammar than an English-style one. Certainly, I don't produce anywhere near as many characters per second, but then again, it generally takes fewer characters to produce a particular meaning as it does in VB. Besides, do you really type code at breakneck pace? I spend most of my time thinking about what I'm trying to say, just as I do when typing English posts on a forum. In fact, the only time my typing speed reaches anywhere near my ability is when I'm simply transcribing prose... such as one does in a typing speed test.

    As for the semicolon, I find it gives one a nice freedom to arrange single statements or expressions over multiple lines, as well as being elegantly analogous to punctuation in poetry. The line continuation character in VB is decidedly clumsy in comparison. It is possible to create a language that requires neither: its name is JScript, and after the second or third time finding a problem that mandatory semicolons would have highlighted instantly, it becomes rather tiresome.

  28. #28
    Moderator
    Join Date
    Jan 2005
    Location
    Sydney
    Posts
    13,621

    Re: Finally on the verge to decide whether C# or Visual Basic?

    To the OP:

    Since you're totally new to programming, I would start with learning how a shell works, and then moving on to shell scripting. This will teach you fundamental concepts like input and output, sequence, selection, iteration, and so on. These are tenets which hold true in all programming languages.

    After that, I suggest C. (Not C++, although you can use a C++ compiler.)
    Then C++ or a .NET language. At this point, I would introduce SQL too, and build a simple application backed by a database.

    As others have said, learning VB6 won't teach you anything that other more relevant languages won't.

  29. #29
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by lehgzil View Post
    in c++

    #include <stdio.h>

    int main()
    {
    printf("Hello World");
    }
    Now show us the GUI version (with a window) and MessageBox for C++

  30. #30
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,054

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by penagate View Post
    Certainly, I don't produce anywhere near as many characters per second, but then again, it generally takes fewer characters to produce a particular meaning as it does in VB. Besides, do you really type code at breakneck pace?
    Actually, yes I do. When I write, I write fast. The thinking goes on between phrases, not within any one phrase. I can see that it wouldn't make any difference if you are thinking about the line as you are writing, but for me, the line, or block, is an atomic unit. Once conceived, the less time I can spend conveying the thought to the screen, the quicker I can move to the next thought.

    In fact, the only time my typing speed reaches anywhere near my ability is when I'm simply transcribing prose... such as one does in a typing speed test.
    I type design notes, unless they require graphics. I find that I can type FAR faster than I can write by hand, and it is often a good idea to have notes on a design, so I just write them out as kind of a free-association so that I can later follow the arc of the concept. I suspect that I do considerably more typing than the average person, largely for this reason (and also because I was a report-writing machine as a biologist, unfortunately, but that was what was needed, even though the reports themselves were largely without value).

    As for the semicolon, I find it gives one a nice freedom to arrange single statements or expressions over multiple lines, as well as being elegantly analogous to punctuation in poetry. The line continuation character in VB is decidedly clumsy in comparison.
    Code as poetry. Still, I don't use semicolons in poetry, either.

    I have seen this argument, but the examples of where it looks so good appear largely contrived and dated. To the contrary, I don't know anybody who has ever done any coding in C who has not spent time tracking down a bug that turned out to be the result of an improper semicolon. Code that is beautiful, even if it comes at the cost of bugs? No thanks. We can do better, as your next comment almost hit on.

    It is possible to create a language that requires neither: its name is JScript
    It is also VB. The line continuation character was deprecated with VS2010. You can now write multi-line VB statements that format nicely, don't need semi-colons, don't need line continuation characters, and don't result in the kinds of bugs you describe for JavaScript. It's also a feature that is nearly as infrequently used as the line continuation character by all except hard-core lambda writers.
    My usual boring signature: Nothing

  31. #31
    Member
    Join Date
    Mar 2011
    Posts
    54

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by cicatrix View Post
    Now show us the GUI version (with a window) and MessageBox for C++
    ~liz
    ('@ ' )~

  32. #32
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    12,892

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by cicatrix View Post
    != vs <> is quite frustrating, by the way
    The OP is lucky (imho) that he isn't learning Java, I find it a confusing language.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  33. #33
    PowerPoster
    Join Date
    May 2002
    Posts
    25,297

    Re: Finally on the verge to decide whether C# or Visual Basic?

    That's how I feel about Python...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  34. #34
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Finally on the verge to decide whether C# or Visual Basic?

    That's how I feel about Prolog...
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  35. #35
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    3,326

    Re: Finally on the verge to decide whether C# or Visual Basic?

    That's how I feel about Mandarin.
    When one of my minions says, "Hey, he's just one guy, what can he do?" I say "This"... and shoot them.

    The problem with putting your lair in a volcano is keeping your robot army from melting.

    I know that the human being and the fish can coexist peacefully - George Bush

  36. #36
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Finally on the verge to decide whether C# or Visual Basic?

    Quote Originally Posted by Shaggy Hiker View Post
    That actually points out the subtle problem with C style languages in general: They make excessive use of very hard to type operator combinations. The semicolon is one of the easiest (although it is totally unnecessary and does nothing but cause bugs anymore), but != is VASTLY harder than <>, as the former stretches both of your hands. The {} in C languages has the same issue, as do most of the other operators. Of course, most of the operators are used in VB, too, including >>,<<,+,-,*,&,^,\,/,(, and ). VB doesn't use the !, though, nor the |, and not much of the ? or :, and it uses much less of the * and &.

    VB is a better typist language.
    I think I agree with everything you've said in this post. Well done.

  37. #37
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,074

    Re: Finally on the verge to decide whether C# or Visual Basic?

    I'm currently in the works on making a Massive DirectX Tutorial Targeting all languages and doing everything to produce the exact program and syntax. Check this out:

    VB6 - DX8
    vb Code:
    1. Option Explicit
    2.  
    3. Private Type CUSTOM_VERTEX
    4.  
    5.     X As Single
    6.     Y As Single
    7.     Z  As Single
    8.     Color As Long
    9.    
    10. End Type
    11.  
    12. Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZ Or D3DFVF_DIFFUSE
    13.  
    14. Private Const PI As Single = 3.141592654
    15. Private Const FOV As Single = PI / 4
    16. Private Const ASPECT_RATIO As Single = 3 / 4
    17. Private Const NEAR_Z As Single = 1
    18. Private Const FAR_Z As Single = 10000
    19.  
    20. Private DX As DirectX8
    21. Private D3D As Direct3D8
    22. Private Device As Direct3DDevice8
    23. Private Display_Mode As D3DDISPLAYMODE
    24. Private Screen As D3DPRESENT_PARAMETERS
    25.  
    26. Private DI As DirectInput8
    27.  
    28. Private Mouse_Device As DirectInputDevice8
    29. Private Mouse_State As DIMOUSESTATE
    30. Private Mouse As D3DVECTOR2
    31.  
    32. Private Keyboard_Device As DirectInputDevice8
    33. Private Keyboard_State As DIKEYBOARDSTATE
    34.  
    35. Private Vertex_List(3) As CUSTOM_VERTEX
    36.  
    37. Private View_Matrix As D3DMATRIX
    38. Private Projection_Matrix As D3DMATRIX
    39.  
    40. Private Camera_Position As D3DVECTOR
    41. Private Camera_Angle As D3DVECTOR
    42.  
    43. Private Fullscreen_Enabled As Boolean
    44. Private Running As Boolean
    45.  
    46. Private Sub DirectX_Initialize()
    47.  
    48.     Set DX = New DirectX8
    49.     Set D3D = DX.Direct3DCreate()
    50.    
    51.     If Fullscreen_Enabled = True Then
    52.         Display_Mode.Width = 800
    53.         Display_Mode.Height = 600
    54.         Display_Mode.Format = D3DFMT_R5G6B5
    55.         Screen.Windowed = False
    56.         Screen.BackBufferCount = 1
    57.         Screen.BackBufferWidth = Display_Mode.Width
    58.         Screen.BackBufferHeight = Display_Mode.Height
    59.         Screen.hDeviceWindow = frmMain.hWnd
    60.     Else
    61.         D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode
    62.         Screen.Windowed = True
    63.     End If
    64.    
    65.     Screen.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
    66.     Screen.BackBufferFormat = Display_Mode.Format
    67.     Screen.AutoDepthStencilFormat = D3DFMT_D16
    68.     Screen.EnableAutoDepthStencil = 1
    69.  
    70.     Set Device = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Screen)
    71.    
    72. End Sub
    73.  
    74. Private Sub DirectInput_Initialize()
    75.  
    76.     Set DI = DX.DirectInputCreate
    77.  
    78. End Sub
    79.  
    80. Private Sub DirectInput_Initialize_Mouse()
    81.  
    82.     Set Mouse_Device = DI.CreateDevice("GUID_SYSMOUSE")
    83.     Mouse_Device.SetCommonDataFormat DIFORMAT_MOUSE
    84.     Mouse_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    85.     Mouse_Device.Acquire
    86.    
    87. End Sub
    88.  
    89. Private Sub Mouse_Controls()
    90.  
    91.     Mouse_Device.GetDeviceStateMouse Mouse_State
    92.    
    93.     Mouse.X = Mouse.X + Mouse_State.lX
    94.     Mouse.Y = Mouse.Y + Mouse_State.lY
    95.    
    96.     If Mouse.X > frmMain.ScaleWidth Then Mouse.X = frmMain.ScaleWidth
    97.     If Mouse.X < 0 Then Mouse.X = 0
    98.    
    99.     If Mouse.Y > frmMain.ScaleHeight Then Mouse.Y = frmMain.ScaleHeight
    100.     If Mouse.Y < 0 Then Mouse.Y = 0
    101.    
    102.     Camera_Angle.X = Camera_Angle.X + Mouse_State.lY
    103.     Camera_Angle.Y = Camera_Angle.Y + Mouse_State.lX
    104.  
    105. End Sub
    106.  
    107. Private Sub DirectInput_Initialize_Keyboard()
    108.    
    109.     Set Keyboard_Device = DI.CreateDevice("GUID_SysKeyboard")
    110.     Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD
    111.     Keyboard_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    112.     Keyboard_Device.Acquire
    113.  
    114. End Sub
    115.  
    116. Private Function DirectInput_Key_State(Key_Code As Long) As Boolean
    117.  
    118.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    119.     DirectInput_Key_State = Keyboard_State.Key(Key_Code) And &H80
    120.  
    121. End Function
    122.  
    123. Private Sub Keyboard_Controls()
    124.    
    125.     Const Camera_Speed As Long = 5
    126.    
    127.     If DirectInput_Key_State(DIK_W) Then 'Move Forward
    128.         Camera_Position.X = Camera_Position.X - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    129.         Camera_Position.Z = Camera_Position.Z - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    130.     End If
    131.    
    132.     If DirectInput_Key_State(DIK_S) Then 'Move Backward
    133.         Camera_Position.X = Camera_Position.X + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    134.         Camera_Position.Z = Camera_Position.Z + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    135.     End If
    136.    
    137.     If DirectInput_Key_State(DIK_A) Then 'Move Left
    138.         Camera_Position.X = Camera_Position.X + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    139.         Camera_Position.Z = Camera_Position.Z - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    140.     End If
    141.    
    142.     If DirectInput_Key_State(DIK_D) Then 'Move Right
    143.         Camera_Position.X = Camera_Position.X - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    144.         Camera_Position.Z = Camera_Position.Z + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    145.     End If
    146.    
    147.     If DirectInput_Key_State(DIK_Q) Then 'Look Left
    148.         Camera_Angle.Y = Camera_Angle.Y - 1
    149.     End If
    150.    
    151.     If DirectInput_Key_State(DIK_E) Then 'Look Right
    152.         Camera_Angle.Y = Camera_Angle.Y + 1
    153.     End If
    154.    
    155.     If DirectInput_Key_State(DIK_R) Then 'Look Up
    156.         Camera_Angle.X = Camera_Angle.X - 1
    157.     End If
    158.    
    159.     If DirectInput_Key_State(DIK_F) Then 'Look Down
    160.         Camera_Angle.X = Camera_Angle.X + 1
    161.     End If
    162.    
    163.     If DirectInput_Key_State(DIK_C) Then 'Move Up
    164.         Camera_Position.Y = Camera_Position.Y - Camera_Speed
    165.     End If
    166.    
    167.     If DirectInput_Key_State(DIK_Z) Then 'Move Down
    168.         Camera_Position.Y = Camera_Position.Y + Camera_Speed
    169.     End If
    170.  
    171. End Sub
    172.  
    173. Private Sub Settings()
    174.  
    175.     Device.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE
    176.     Device.SetRenderState D3DRS_ZWRITEENABLE, 1
    177.     Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
    178.     Device.SetRenderState D3DRS_LIGHTING, 0
    179.  
    180. End Sub
    181.  
    182. Private Function Create_Vertex(X As Single, Y As Single, Z As Single) As D3DVECTOR
    183.  
    184.     Create_Vertex.X = X
    185.     Create_Vertex.Y = Y
    186.     Create_Vertex.Z = Z
    187.    
    188. End Function
    189.  
    190. Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Long) As CUSTOM_VERTEX
    191.  
    192.     Create_Custom_Vertex.X = X
    193.     Create_Custom_Vertex.Y = Y
    194.     Create_Custom_Vertex.Z = Z
    195.     Create_Custom_Vertex.Color = Color
    196.    
    197. End Function
    198.  
    199. Private Sub Setup_Matrices()
    200.    
    201.     D3DXMatrixLookAtRH View_Matrix, Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0)
    202.     Device.SetTransform D3DTS_VIEW, View_Matrix
    203.    
    204.     D3DXMatrixPerspectiveFovRH Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z
    205.     Device.SetTransform D3DTS_PROJECTION, Projection_Matrix
    206.  
    207. End Sub
    208.  
    209. Public Sub Camera_Control()
    210.  
    211.     Dim Camera_Translation_Matrix As D3DMATRIX
    212.     Dim Camera_Angle_Matrix_X As D3DMATRIX
    213.     Dim Camera_Angle_Matrix_Y As D3DMATRIX
    214.    
    215.     D3DXMatrixIdentity View_Matrix
    216.     D3DXMatrixIdentity Camera_Translation_Matrix
    217.    
    218.     D3DXMatrixTranslation Camera_Translation_Matrix, Camera_Position.X, Camera_Position.Y, -Camera_Position.Z
    219.     D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Translation_Matrix
    220.  
    221.     D3DXMatrixRotationY Camera_Angle_Matrix_Y, (PI * Camera_Angle.Y) / 180
    222.     D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_Y
    223.  
    224.     D3DXMatrixRotationX Camera_Angle_Matrix_X, (PI * Camera_Angle.X) / 180
    225.     D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_X
    226.    
    227.     Device.SetTransform D3DTS_VIEW, View_Matrix
    228.    
    229. End Sub
    230.  
    231. Private Sub Create_Polygon()
    232.  
    233.     Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, D3DColorXRGB(255, 255, 255))
    234.     Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, D3DColorXRGB(255, 255, 255))
    235.     Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, D3DColorXRGB(255, 255, 255))
    236.     Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, D3DColorXRGB(255, 255, 255))
    237.  
    238. End Sub
    239.  
    240. Private Sub Draw_Polygon()
    241.  
    242.     Device.SetVertexShader CUSTOM_VERTEX_FORMAT
    243.     Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
    244.  
    245. End Sub
    246.  
    247. Private Sub Create_Platform()
    248.  
    249.     Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, D3DColorXRGB(255, 0, 0))
    250.     Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, D3DColorXRGB(0, 255, 0))
    251.     Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, D3DColorXRGB(0, 0, 255))
    252.     Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, D3DColorXRGB(255, 0, 255))
    253.  
    254. End Sub
    255.  
    256. Private Sub Render()
    257.  
    258.     Device.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, D3DColorXRGB(0, 0, 0), 1, 0
    259.     Device.BeginScene
    260.     'Rendering code goes here
    261.     Create_Platform
    262.     Draw_Polygon
    263.     Create_Polygon
    264.     Draw_Polygon
    265.     Device.EndScene
    266.     Device.Present ByVal 0, ByVal 0, 0, ByVal 0
    267.  
    268. End Sub
    269.  
    270. Private Sub Game_Loop()
    271.  
    272.     Do While Running = True
    273.         Mouse_Controls
    274.         Keyboard_Controls
    275.         Camera_Control
    276.         Render
    277.         If DirectInput_Key_State(DIK_ESCAPE) <> 0 Then Shutdown
    278.         DoEvents
    279.     Loop
    280.  
    281. End Sub
    282.  
    283. Private Sub Main()
    284.  
    285.     If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then
    286.         Fullscreen_Enabled = True
    287.     End If
    288.    
    289.     With Me
    290.         .Show
    291.         .ScaleMode = vbPixels
    292.         .ScaleWidth = 312
    293.         .ScaleHeight = 213
    294.         .Caption = "DirectX Tutorial"
    295.         If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone
    296.     End With
    297.    
    298.     Camera_Position.X = 0
    299.     Camera_Position.Y = -50
    300.     Camera_Position.Z = 250
    301.    
    302.     DirectX_Initialize
    303.     DirectInput_Initialize
    304.     DirectInput_Initialize_Mouse
    305.     DirectInput_Initialize_Keyboard
    306.     Settings
    307.     Setup_Matrices
    308.     Running = True
    309.     Game_Loop
    310.    
    311. End Sub
    312.  
    313. Private Sub Shutdown()
    314.  
    315.     If Running = True Then
    316.         Running = False
    317.         Mouse_Device.Unacquire
    318.         Set Mouse_Device = Nothing
    319.         Keyboard_Device.Unacquire
    320.         Set Keyboard_Device = Nothing
    321.         Set DI = Nothing
    322.         Set Device = Nothing
    323.         Set D3D = Nothing
    324.         Set DX = Nothing
    325.         Unload Me
    326.     End If
    327.  
    328. End Sub
    329.  
    330. Private Sub Form_Load()
    331.  
    332.     Main
    333.  
    334. End Sub
    335.  
    336. Private Sub Form_Unload(Cancel As Integer)
    337.  
    338.     Shutdown
    339.  
    340. End Sub

    CONTINUED NEXT POST...

  38. #38
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,074

    Re: Finally on the verge to decide whether C# or Visual Basic?

    CONTINUED FROM LAST POST...

    VB.NET - DX9
    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.DirectX
    5. Imports Microsoft.DirectX.Direct3D
    6. Imports Microsoft.DirectX.DirectInput
    7.  
    8. Public Class frmMain
    9.  
    10.     Private Const FOV As Single = Math.PI / 4
    11.     Private Const ASPECT_RATIO As Single = 4 / 3
    12.     Private Const NEAR_Z As Single = 1
    13.     Private Const FAR_Z As Single = 10000
    14.  
    15.     Private Device As Direct3D.Device
    16.     Private Display_Mode As DisplayMode
    17.     Private Screen As New PresentParameters
    18.  
    19.     Private Mouse_Device As DirectInput.Device
    20.     Private Mouse_State As MouseState
    21.     Private Mouse As Vector2
    22.  
    23.     Private Keyboard_Device As DirectInput.Device
    24.     Private Keyboard_State As KeyboardState
    25.  
    26.     Private Vertex_List(3) As CustomVertex.PositionColored
    27.  
    28.     Private View_Matrix As Matrix
    29.     Private Projection_Matrix As Matrix
    30.  
    31.     Private Camera_Position As Vector3
    32.     Private Camera_Angle As Vector3
    33.  
    34.     Private Fullscreen_Enabled As Boolean
    35.     Private Running As Boolean
    36.  
    37.     Private Sub DirectX_Initialize()
    38.  
    39.         If Fullscreen_Enabled = True Then
    40.             Display_Mode.Width = 800
    41.             Display_Mode.Height = 600
    42.             Display_Mode.Format = Direct3D.Format.R5G6B5
    43.             Screen.Windowed = False
    44.             Screen.BackBufferCount = 1
    45.             Screen.BackBufferWidth = Display_Mode.Width
    46.             Screen.BackBufferHeight = Display_Mode.Height
    47.         Else
    48.             Screen.Windowed = True
    49.         End If
    50.  
    51.         Screen.SwapEffect = SwapEffect.Copy
    52.         Screen.BackBufferFormat = Display_Mode.Format
    53.         Screen.AutoDepthStencilFormat = DepthFormat.D16
    54.         Screen.EnableAutoDepthStencil = True
    55.  
    56.         Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    57.  
    58.     End Sub
    59.  
    60.     Private Sub DirectInput_Initialize_Mouse()
    61.  
    62.         Mouse_Device = New DirectInput.Device(SystemGuid.Mouse)
    63.         Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse)
    64.         Mouse_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    65.         Mouse_Device.Acquire()
    66.  
    67.     End Sub
    68.  
    69.     Private Sub Mouse_Controls()
    70.  
    71.         Mouse_State = Mouse_Device.CurrentMouseState
    72.  
    73.         Mouse.X = Mouse.X + Mouse_State.X
    74.         Mouse.Y = Mouse.Y + Mouse_State.Y
    75.  
    76.         If Mouse.X > Me.Width Then Mouse.X = Me.Width
    77.         If Mouse.X < 0 Then Mouse.X = 0
    78.  
    79.         If Mouse.Y > Me.Height Then Mouse.Y = Me.Height
    80.         If Mouse.Y < 0 Then Mouse.Y = 0
    81.  
    82.         Camera_Angle.X += Mouse_State.Y
    83.         Camera_Angle.Y += Mouse_State.X
    84.  
    85.         If Camera_Angle.X >= 90 Then Camera_Angle.X = 90
    86.         If Camera_Angle.X <= -90 Then Camera_Angle.X = -90
    87.  
    88.     End Sub
    89.  
    90.     Private Sub DirectInput_Initialize_Keyboard()
    91.  
    92.         Keyboard_Device = New DirectInput.Device(SystemGuid.Keyboard)
    93.         Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard)
    94.         Keyboard_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    95.         Keyboard_Device.Acquire()
    96.  
    97.     End Sub
    98.  
    99.     Private Function DirectInput_Key_State(ByVal Key_Code As DirectInput.Key) As Boolean
    100.  
    101.         Keyboard_State = Keyboard_Device.GetCurrentKeyboardState
    102.         Return Keyboard_State.Item(Key_Code)
    103.  
    104.     End Function
    105.  
    106.     Private Sub Keyboard_Controls()
    107.  
    108.         Const Camera_Speed As Long = 5
    109.  
    110.         If DirectInput_Key_State(Key.W) Then 'Move Forward
    111.             Camera_Position.X -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    112.             Camera_Position.Z -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    113.         End If
    114.  
    115.         If DirectInput_Key_State(Key.S) Then 'Move Backward
    116.             Camera_Position.X += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    117.             Camera_Position.Z += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    118.         End If
    119.  
    120.         If DirectInput_Key_State(Key.A) Then 'Move Left
    121.             Camera_Position.X += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    122.             Camera_Position.Z -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    123.         End If
    124.  
    125.         If DirectInput_Key_State(Key.D) Then 'Move Right
    126.             Camera_Position.X -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    127.             Camera_Position.Z += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    128.         End If
    129.  
    130.         If DirectInput_Key_State(Key.Q) Then 'Look Left
    131.             Camera_Angle.Y -= 1
    132.         End If
    133.  
    134.         If DirectInput_Key_State(Key.E) Then 'Look Right
    135.             Camera_Angle.Y += 1
    136.         End If
    137.  
    138.         If DirectInput_Key_State(Key.R) Then 'Look Up
    139.             Camera_Angle.X -= 1
    140.         End If
    141.  
    142.         If DirectInput_Key_State(Key.F) Then 'Look Down
    143.             Camera_Angle.X += 1
    144.         End If
    145.  
    146.         If DirectInput_Key_State(Key.C) Then 'Move Up
    147.             Camera_Position.Y -= Camera_Speed
    148.         End If
    149.  
    150.         If DirectInput_Key_State(Key.Z) Then 'Move Down
    151.             Camera_Position.Y += Camera_Speed
    152.         End If
    153.  
    154.     End Sub
    155.  
    156.     Private Sub Settings()
    157.  
    158.         Device.SetRenderState(RenderStates.ZEnable, True)
    159.         Device.SetRenderState(RenderStates.ZBufferWriteEnable, True)
    160.         Device.SetRenderState(RenderStates.CullMode, Cull.None)
    161.         Device.SetRenderState(RenderStates.Lighting, False)
    162.  
    163.     End Sub
    164.  
    165.     Private Function Create_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Vector3
    166.  
    167.         Dim Vertex As Vector3
    168.  
    169.         Vertex.X = X
    170.         Vertex.Y = Y
    171.         Vertex.Z = Z
    172.  
    173.         Return Vertex
    174.  
    175.     End Function
    176.  
    177.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer) As CustomVertex.PositionColored
    178.  
    179.         Dim Vertex As CustomVertex.PositionColored = New CustomVertex.PositionColored
    180.  
    181.         Vertex.Position = New Vector3(X, Y, Z)
    182.         Vertex.Color = Color
    183.  
    184.         Return Vertex
    185.  
    186.     End Function
    187.  
    188.     Private Sub Setup_Matrices()
    189.  
    190.         View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0))
    191.         Device.Transform.View = View_Matrix
    192.  
    193.         Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z)
    194.         Device.Transform.Projection = Projection_Matrix
    195.  
    196.     End Sub
    197.  
    198.     Private Sub Camera_Control()
    199.  
    200.         Dim Camera_Translation_Matrix As Matrix
    201.         Dim Camera_Angle_Matrix_X As Matrix
    202.         Dim Camera_Angle_Matrix_Y As Matrix
    203.  
    204.         View_Matrix = Matrix.Identity
    205.         Camera_Translation_Matrix = Matrix.Identity
    206.  
    207.         Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z)
    208.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix)
    209.  
    210.         Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180)
    211.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y)
    212.  
    213.         Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180)
    214.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X)
    215.  
    216.         Device.Transform.View = View_Matrix
    217.  
    218.     End Sub
    219.  
    220.     Private Sub Create_Polygon()
    221.  
    222.         Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
    223.         Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
    224.         Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
    225.         Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
    226.  
    227.     End Sub
    228.  
    229.     Private Sub Draw_Polygon()
    230.  
    231.         Device.VertexFormat = CustomVertex.PositionColored.Format
    232.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    233.  
    234.     End Sub
    235.  
    236.     Private Sub Create_Platform()
    237.  
    238.         Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb)
    239.         Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb)
    240.         Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb)
    241.         Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb)
    242.  
    243.     End Sub
    244.  
    245.     Private Sub Reset_Device()
    246.  
    247.         If Me.WindowState <> FormWindowState.Minimized And Fullscreen_Enabled = False Then
    248.             Running = False
    249.             Device.Reset(Screen)
    250.             Settings()
    251.             Setup_Matrices()
    252.             Application.DoEvents()
    253.             Running = True
    254.         End If
    255.  
    256.     End Sub
    257.  
    258.     Private Sub Render()
    259.  
    260.         Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1, 0)
    261.         Device.BeginScene()
    262.         Create_Polygon()
    263.         Draw_Polygon()
    264.         Create_Platform()
    265.         Draw_Polygon()
    266.         Device.EndScene()
    267.         Device.Present()
    268.  
    269.     End Sub
    270.  
    271.     Private Sub Game_Loop()
    272.  
    273.         Do While Running = True
    274.             Mouse_Controls()
    275.             Keyboard_Controls()
    276.             Camera_Control()
    277.             Render()
    278.             If DirectInput_Key_State(Key.Escape) Then Shutdown()
    279.             Application.DoEvents()
    280.         Loop
    281.  
    282.     End Sub
    283.  
    284.     Private Sub Main()
    285.  
    286.         If MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    287.             Fullscreen_Enabled = True
    288.         End If
    289.  
    290.         With Me
    291.             .Show()
    292.             .Width = 330
    293.             .Height = 250
    294.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
    295.             .Text = "DirectX Tutorial"
    296.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    297.         End With
    298.  
    299.         Camera_Position.X = 0
    300.         Camera_Position.Y = -50
    301.         Camera_Position.Z = 250
    302.  
    303.         DirectX_Initialize()
    304.         DirectInput_Initialize_Mouse()
    305.         DirectInput_Initialize_Keyboard()
    306.         Settings()
    307.         Setup_Matrices()
    308.         Running = True
    309.  
    310.     End Sub
    311.  
    312.     Private Sub Shutdown()
    313.  
    314.         If Running = True Then
    315.             Running = False
    316.             Mouse_Device.Unacquire()
    317.             Mouse_Device.Dispose()
    318.             Keyboard_Device.Unacquire()
    319.             Keyboard_Device.Dispose()
    320.             Device.Dispose()
    321.             Application.Exit()
    322.         End If
    323.  
    324.     End Sub
    325.  
    326.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    327.  
    328.         Main()
    329.  
    330.     End Sub
    331.  
    332.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    333.  
    334.         Shutdown()
    335.  
    336.     End Sub
    337.  
    338.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    339.  
    340.         Game_Loop()
    341.  
    342.     End Sub
    343.  
    344.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    345.  
    346.         Reset_Device()
    347.  
    348.     End Sub
    349.  
    350. End Class

    CONTINUED NEXT POST...

  39. #39
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,074

    Re: Finally on the verge to decide whether C# or Visual Basic?

    CONTINUED FROM LAST POST...
    C# - DX9
    c# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using Microsoft.DirectX;
    10. using Microsoft.DirectX.Direct3D;
    11. using Microsoft.DirectX.DirectInput;
    12.  
    13. namespace DX_Tut
    14. {
    15.     public partial class frmMain : Form
    16.     {
    17.         private const float FOV = (float)Math.PI / 4.0f;
    18.         private const float ASPECT_RATIO = 4.0f / 3.0f;
    19.         private const float NEAR_Z = 1.0f;
    20.         private const float FAR_Z = 10000.0f;
    21.  
    22.         Microsoft.DirectX.Direct3D.Device Device;
    23.         DisplayMode Display_Mode;
    24.         PresentParameters Screen;
    25.  
    26.         Microsoft.DirectX.DirectInput.Device Mouse_Device;
    27.         MouseState Mouse_State;
    28.         Vector2 Mouse;
    29.  
    30.         Microsoft.DirectX.DirectInput.Device Keyboard_Device;
    31.         KeyboardState Keyboard_State;
    32.  
    33.         CustomVertex.PositionColored[] Vertex_List = new CustomVertex.PositionColored[4];
    34.  
    35.         Matrix View_Matrix;
    36.         Matrix Projection_Matrix;
    37.  
    38.         Vector3 Camera_Position;
    39.         Vector3 Camera_Angle;
    40.  
    41.         bool Fullscreen_Enabled;
    42.         bool Running;
    43.  
    44.         private void DirectX_Initialize()
    45.         {
    46.             Screen = new PresentParameters();
    47.             if (Fullscreen_Enabled == true)
    48.             {
    49.                 Display_Mode.Width = 800;
    50.                 Display_Mode.Height = 600;
    51.                 Display_Mode.Format = Format.R5G6B5;
    52.                 Screen.Windowed = false;
    53.                 Screen.BackBufferCount = 1;
    54.                 Screen.BackBufferWidth = Display_Mode.Width;
    55.                 Screen.BackBufferHeight = Display_Mode.Height;
    56.             }
    57.             else
    58.             {
    59.                 Screen.Windowed = true;
    60.             }
    61.             Screen.SwapEffect = SwapEffect.Copy;
    62.             Screen.BackBufferFormat = Display_Mode.Format;
    63.             Screen.AutoDepthStencilFormat = DepthFormat.D16;
    64.             Screen.EnableAutoDepthStencil = true;
    65.  
    66.             Device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen);
    67.         }
    68.  
    69.         private void DirectInput_Initialize_Mouse()
    70.         {
    71.             Mouse_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);
    72.             Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse);
    73.             Mouse_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
    74.             Mouse_Device.Acquire();
    75.         }
    76.  
    77.         private void Mouse_Controls()
    78.         {
    79.             Mouse_State = Mouse_Device.CurrentMouseState;
    80.  
    81.             Mouse.X += Mouse_State.X;
    82.             Mouse.Y += Mouse_State.Y;
    83.  
    84.             if (Mouse.X > this.Width) Mouse.X = this.Width;
    85.             if (Mouse.X < 0) Mouse.X = 0;
    86.  
    87.             if (Mouse.Y > this.Height) Mouse.Y = this.Height;
    88.             if (Mouse.Y < 0) Mouse.Y = 0;
    89.  
    90.             Camera_Angle.X += Mouse_State.Y;
    91.             Camera_Angle.Y += Mouse_State.X;
    92.  
    93.             if (Camera_Angle.X >= 90) Camera_Angle.X = 90;
    94.             if (Camera_Angle.X <= -90) Camera_Angle.X = -90;
    95.         }
    96.  
    97.         private void DirectInput_Initialize_Keyboard()
    98.         {
    99.             Keyboard_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
    100.             Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard);
    101.             Keyboard_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
    102.             Keyboard_Device.Acquire();
    103.         }
    104.  
    105.         private bool DirectInput_Key_State(Microsoft.DirectX.DirectInput.Key Key_Code)
    106.         {
    107.             Keyboard_State = Keyboard_Device.GetCurrentKeyboardState();
    108.             return Keyboard_State[Key_Code];
    109.         }
    110.  
    111.         private void Keyboard_Controls()
    112.         {
    113.             const int Camera_Speed = 5;
    114.  
    115.             if (DirectInput_Key_State(Key.W)) //Move Forward
    116.             {
    117.                 Camera_Position.X -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    118.                 Camera_Position.Z -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    119.             }
    120.  
    121.             if (DirectInput_Key_State(Key.S)) //Move Backward
    122.             {
    123.                 Camera_Position.X += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    124.                 Camera_Position.Z += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    125.             }
    126.  
    127.             if (DirectInput_Key_State(Key.A)) //Move Left
    128.             {
    129.                 Camera_Position.X += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    130.                 Camera_Position.Z -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    131.             }
    132.  
    133.             if (DirectInput_Key_State(Key.D)) //Move Right
    134.             {
    135.                 Camera_Position.X -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    136.                 Camera_Position.Z += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    137.             }
    138.  
    139.             if (DirectInput_Key_State(Key.Q)) //Look Left
    140.                 Camera_Angle.Y -= 1;
    141.  
    142.             if (DirectInput_Key_State(Key.E)) //Look Right
    143.                 Camera_Angle.Y += 1;
    144.  
    145.             if (DirectInput_Key_State(Key.R)) //Look Up
    146.                 Camera_Angle.X -= 1;
    147.  
    148.             if (DirectInput_Key_State(Key.F)) //Look Down
    149.                 Camera_Angle.X += 1;
    150.  
    151.             if (DirectInput_Key_State(Key.C)) //Move Up
    152.                 Camera_Position.Y -= Camera_Speed;
    153.  
    154.             if (DirectInput_Key_State(Key.Z)) //Move Down
    155.                 Camera_Position.Y += Camera_Speed;
    156.         }
    157.  
    158.         private void Settings()
    159.         {
    160.             Device.SetRenderState(RenderStates.ZEnable, true);
    161.             Device.SetRenderState(RenderStates.ZBufferWriteEnable, true);
    162.             Device.SetRenderState(RenderStates.CullMode, (int)Cull.None);
    163.             Device.SetRenderState(RenderStates.Lighting, false);
    164.         }
    165.  
    166.         private Vector3 Create_Vertex(float X, float Y, float Z)
    167.         {
    168.             Vector3 Vertex;
    169.  
    170.             Vertex.X = X;
    171.             Vertex.Y = Y;
    172.             Vertex.Z = Z;
    173.  
    174.             return Vertex;
    175.         }
    176.  
    177.         private CustomVertex.PositionColored Create_Custom_Vertex(float X, float Y, float Z, int Color)
    178.         {
    179.             CustomVertex.PositionColored Vertex = new CustomVertex.PositionColored();
    180.             Vertex.Position = new Vector3(X, Y, Z);
    181.             Vertex.Color = Color;
    182.  
    183.             return Vertex;
    184.         }
    185.  
    186.         private void Setup_Matrices()
    187.         {
    188.             View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0));
    189.             Device.Transform.View = View_Matrix;
    190.  
    191.             Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
    192.             Device.Transform.Projection = Projection_Matrix;
    193.         }
    194.  
    195.         private void Camera_Control()
    196.         {
    197.             Matrix Camera_Translation_Matrix;
    198.             Matrix Camera_Angle_Matrix_X;
    199.             Matrix Camera_Angle_Matrix_Y;
    200.  
    201.             View_Matrix = Matrix.Identity;
    202.             Camera_Translation_Matrix = Matrix.Identity;
    203.  
    204.             Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z);
    205.             View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix);
    206.  
    207.             Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180);
    208.             View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y);
    209.  
    210.             Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180);
    211.             View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X);
    212.  
    213.             Device.Transform.View = View_Matrix;
    214.         }
    215.  
    216.         private void Create_Polygon()
    217.         {
    218.             Vertex_List[0] = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb());
    219.             Vertex_List[1] = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb());
    220.             Vertex_List[2] = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb());
    221.             Vertex_List[3] = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb());
    222.         }
    223.  
    224.         private void Draw_Polygon()
    225.         {
    226.             Device.VertexFormat = CustomVertex.PositionColored.Format;
    227.             Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List);
    228.         }
    229.  
    230.         private void Create_Platform()
    231.         {
    232.             Vertex_List[0] = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb());
    233.             Vertex_List[1] = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb());
    234.             Vertex_List[2] = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb());
    235.             Vertex_List[3] = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb());
    236.         }
    237.  
    238.         private void Reset_Device()
    239.         {
    240.             if (this.WindowState != FormWindowState.Minimized && Fullscreen_Enabled == false)
    241.             {
    242.                 Running = false;
    243.                 Device.Reset(Screen);
    244.                 Settings();
    245.                 Setup_Matrices();
    246.                 Application.DoEvents();
    247.                 Running = true;
    248.             }
    249.         }
    250.  
    251.         private void Render()
    252.         {
    253.             Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1.0f, 0);
    254.             Device.BeginScene();
    255.             Create_Platform();
    256.             Draw_Polygon();
    257.             Create_Polygon();
    258.             Draw_Polygon();
    259.             Device.EndScene();
    260.             Device.Present();
    261.         }
    262.  
    263.         private void Game_Loop()
    264.         {
    265.             do
    266.             {
    267.                 Mouse_Controls();
    268.                 Keyboard_Controls();
    269.                 Camera_Control();
    270.                 Render();
    271.                 if (DirectInput_Key_State(Key.Escape)) Shutdown();
    272.                 Application.DoEvents();
    273.             } while (Running == true);
    274.         }
    275.  
    276.         private void Main()
    277.         {
    278.             if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    279.                 Fullscreen_Enabled = true;
    280.  
    281.             this.Show();
    282.             this.KeyPreview = true;
    283.             this.Width = 330;
    284.             this.Height = 250;
    285.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    286.             this.Text = "DirectX Tutorial";
    287.             if (Fullscreen_Enabled == true)
    288.                 this.FormBorderStyle = FormBorderStyle.None;
    289.  
    290.             Camera_Position.X = 0;
    291.             Camera_Position.Y = -50;
    292.             Camera_Position.Z = 250;
    293.  
    294.             DirectX_Initialize();
    295.             DirectInput_Initialize_Mouse();
    296.             DirectInput_Initialize_Keyboard();
    297.             Settings();
    298.             Setup_Matrices();
    299.             Running = true;
    300.         }
    301.  
    302.         void Shutdown()
    303.         {
    304.             if (Running == true)
    305.             {
    306.                 Running = false;
    307.                 Mouse_Device.Unacquire();
    308.                 Mouse_Device.Dispose();
    309.                 Keyboard_Device.Unacquire();
    310.                 Keyboard_Device.Dispose();
    311.                 Device.Dispose();
    312.                 Application.Exit();
    313.             }
    314.         }
    315.  
    316.         public frmMain()
    317.         {
    318.             InitializeComponent();
    319.         }
    320.  
    321.         private void frmMain_Load(object sender, EventArgs e)
    322.         {
    323.             Main();
    324.         }
    325.  
    326.         private void frmMain_Paint(object sender, PaintEventArgs e)
    327.         {
    328.             Game_Loop();
    329.         }
    330.  
    331.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    332.         {
    333.             Shutdown();
    334.         }
    335.  
    336.         private void frmMain_Resize(object sender, EventArgs e)
    337.         {
    338.             Reset_Device();
    339.         }
    340.     }
    341. }

    CONTINUED NEXT POST...

  40. #40
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,074

    Re: Finally on the verge to decide whether C# or Visual Basic?

    CONTINUED FROM LAST POST...

    C++ - DX9
    c++ Code:
    1. #include <windows.h>
    2. #include <d3d9.h>
    3. #include <d3dx9.h>
    4. #include <dinput.h>
    5.  
    6. #pragma comment (lib, "d3d9.lib")
    7. #pragma comment (lib, "d3dx9.lib")
    8. #pragma comment (lib, "dinput8.lib")
    9. #pragma comment (lib, "dxguid.lib")
    10.  
    11. struct CUSTOM_VERTEX
    12. {
    13.     float X, Y, Z;
    14.     DWORD Color;
    15. };
    16.  
    17. #define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE)
    18.  
    19. const float PI = 3.141592654f;
    20. const float FOV = PI / 4.0f;
    21. const float ASPECT_RATIO = 4.0f / 3.0f;
    22. const float NEAR_Z = 1.0f;
    23. const float FAR_Z = 10000.0f;
    24.  
    25. HWND hWnd;
    26. MSG msg;
    27. HINSTANCE hInstance;
    28. int Width;
    29. int Height;
    30.  
    31. LPDIRECT3D9 D3D;
    32. LPDIRECT3DDEVICE9 Device;
    33. D3DDISPLAYMODE Display_Mode;
    34. D3DPRESENT_PARAMETERS Screen;
    35.  
    36. LPDIRECTINPUT8 DI;
    37.  
    38. LPDIRECTINPUTDEVICE8 Mouse_Device;
    39. DIMOUSESTATE Mouse_State;
    40. D3DXVECTOR2 Mouse;
    41.  
    42. LPDIRECTINPUTDEVICE8 Keyboard_Device;
    43. BYTE Keyboard_State[256];
    44.  
    45. CUSTOM_VERTEX Vertex_List[4];
    46.  
    47. D3DXMATRIX View_Matrix;
    48. D3DXMATRIX Projection_Matrix;
    49.  
    50. D3DVECTOR Camera_Position;
    51. D3DVECTOR Camera_Angle;
    52.  
    53. bool Fullscreen_Enabled;
    54. bool Running;
    55.  
    56. void DirectX_Initialize(void);
    57. void DirectInput_Initialize(void);
    58. void DirectInput_Initialize_Mouse(void);
    59. void Mouse_Controls(void);
    60. void DirectInput_Initialize_Keyboard(void);
    61. bool DirectInput_Key_State(void);
    62. void Keyboard_Controls(void);
    63. void Settings(void);
    64. D3DXVECTOR3 Create_Vertex(void);
    65. CUSTOM_VERTEX Create_Custom_Vertex(void);
    66. void Setup_Matrices(void);
    67. void Camera_Control(void);
    68. void Create_Polygon(void);
    69. void Draw_Polygon(void);
    70. void Create_Platform(void);
    71. void Render(void);
    72. void Game_Loop(void);
    73. void Main(void);
    74. void Shutdown(void);
    75.  
    76. LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    77.  
    78. void DirectX_Initialize()
    79. {
    80.     D3D = Direct3DCreate9(D3D_SDK_VERSION);
    81.     memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS));
    82.  
    83.     if (Fullscreen_Enabled == true)
    84.     {
    85.        Display_Mode.Width = 800;
    86.        Display_Mode.Height = 600;
    87.        Display_Mode.Format = D3DFMT_R5G6B5;
    88.        Screen.Windowed = FALSE;
    89.        Screen.BackBufferCount = 1;
    90.        Screen.BackBufferWidth = Display_Mode.Width;
    91.        Screen.BackBufferHeight = Display_Mode.Height;
    92.        Screen.hDeviceWindow = hWnd;
    93.     }
    94.     else
    95.     {
    96.        D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);
    97.        Screen.Windowed = TRUE;
    98.     }
    99.  
    100.     Screen.SwapEffect = D3DSWAPEFFECT_DISCARD;
    101.     Screen.BackBufferFormat = D3DFMT_R5G6B5;
    102.     Screen.EnableAutoDepthStencil = TRUE;
    103.     Screen.AutoDepthStencilFormat = D3DFMT_D16;
    104.  
    105.     D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device);
    106. }
    107.  
    108. void DirectInput_Initialize()
    109. {
    110.     DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL);
    111. }
    112.  
    113. void DirectInput_Initialize_Mouse()
    114. {
    115.     DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL);
    116.     Mouse_Device->SetDataFormat(&c_dfDIMouse);
    117.     Mouse_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
    118.     Mouse_Device->Acquire();
    119. }
    120.  
    121. void Mouse_Controls()
    122. {
    123.     Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);
    124.  
    125.     Mouse.x += Mouse_State.lX;
    126.     Mouse.y += Mouse_State.lY;
    127.    
    128.     if (Mouse.x > Width) Mouse.x = (float)Width;
    129.     if (Mouse.y < 0) Mouse.x = 0;
    130.    
    131.     if (Mouse.x > Height) Mouse.y = (float)Height;
    132.     if (Mouse.y < 0) Mouse.y = 0;
    133.    
    134.     Camera_Angle.x += Mouse_State.lY;
    135.     Camera_Angle.y += Mouse_State.lX;
    136.  
    137.     if (Camera_Angle.x >= 90) Camera_Angle.x = 90;
    138.     if (Camera_Angle.x <= -90) Camera_Angle.x = -90;
    139. }
    140.  
    141. void DirectInput_Initialize_Keyboard()
    142. {
    143.     DI->CreateDevice(GUID_SysKeyboard, &Keyboard_Device, NULL);
    144.     Keyboard_Device->SetDataFormat(&c_dfDIKeyboard);
    145.     Keyboard_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
    146.     Keyboard_Device->Acquire();
    147. }
    148.  
    149. bool DirectInput_Key_State(int Key_Code)
    150. {
    151.     Keyboard_Device->GetDeviceState(256, (LPVOID)Keyboard_State);
    152.     return Keyboard_State[Key_Code] && 0x80;
    153. }
    154.  
    155. void Keyboard_Controls()
    156. {
    157.     const int Camera_Speed = 5;
    158.  
    159.     if (DirectInput_Key_State(DIK_W))
    160.     {
    161.         Camera_Position.x -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    162.         Camera_Position.z -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    163.     }
    164.  
    165.     if (DirectInput_Key_State(DIK_S))
    166.     {
    167.         Camera_Position.x += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    168.         Camera_Position.z += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    169.     }
    170.  
    171.     if (DirectInput_Key_State(DIK_A))
    172.     {
    173.         Camera_Position.x += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    174.         Camera_Position.z -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    175.     }
    176.  
    177.     if (DirectInput_Key_State(DIK_D))
    178.     {
    179.         Camera_Position.x -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    180.         Camera_Position.z += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    181.     }
    182.  
    183.     if (DirectInput_Key_State(DIK_Q))
    184.     {
    185.         Camera_Angle.y -= 1;
    186.     }
    187.  
    188.     if (DirectInput_Key_State(DIK_E))
    189.     {
    190.         Camera_Angle.y += 1;
    191.     }
    192.  
    193.     if (DirectInput_Key_State(DIK_R))
    194.     {
    195.         Camera_Angle.x -= 1;
    196.     }
    197.  
    198.     if (DirectInput_Key_State(DIK_F))
    199.     {
    200.         Camera_Angle.x += 1;
    201.     }
    202.  
    203.     if (DirectInput_Key_State(DIK_C))
    204.     {
    205.         Camera_Position.y -= Camera_Speed;
    206.     }
    207.  
    208.     if (DirectInput_Key_State(DIK_Z))
    209.     {
    210.         Camera_Position.y += Camera_Speed;
    211.     }
    212. }
    213.  
    214. void Settings()
    215. {
    216.     Device->SetRenderState(D3DRS_ZENABLE, TRUE);
    217.     Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
    218.     Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    219.     Device->SetRenderState(D3DRS_LIGHTING, FALSE);
    220. }
    221.  
    222. D3DXVECTOR3 Create_Vertex(float X, float Y, float Z)
    223. {
    224.     D3DXVECTOR3 Vertex;
    225.  
    226.     Vertex.x = X;
    227.     Vertex.y = Y;
    228.     Vertex.z = Z;
    229.  
    230.     return Vertex;
    231. }
    232.  
    233. CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, DWORD Color)
    234. {
    235.     CUSTOM_VERTEX Vertex;
    236.  
    237.     Vertex.X = X;
    238.     Vertex.Y = Y;
    239.     Vertex.Z = Z;
    240.     Vertex.Color = Color;
    241.  
    242.     return Vertex;
    243. }
    244.  
    245. void Setup_Matrices()
    246. {
    247.     D3DXMatrixLookAtRH(&View_Matrix, &Create_Vertex(Camera_Position.x, Camera_Position.y, Camera_Position.z), &Create_Vertex(0, 0, 0), &Create_Vertex(0.0f, 1.0f, 0.0f));
    248.     Device->SetTransform(D3DTS_VIEW, &View_Matrix);
    249.  
    250.     D3DXMatrixPerspectiveFovRH(&Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
    251.     Device->SetTransform(D3DTS_PROJECTION, &Projection_Matrix);
    252. }
    253.  
    254. void Camera_Control()
    255. {
    256.     D3DXMATRIX Camera_Translation_Matrix;
    257.     D3DXMATRIX Camera_Angle_Matrix_X;
    258.     D3DXMATRIX Camera_Angle_Matrix_Y;
    259.    
    260.     D3DXMatrixIdentity(&View_Matrix);
    261.     D3DXMatrixIdentity(&Camera_Translation_Matrix);
    262.  
    263.     D3DXMatrixTranslation(&Camera_Translation_Matrix, Camera_Position.x, Camera_Position.y, -Camera_Position.z);
    264.     D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Translation_Matrix);
    265.  
    266.     D3DXMatrixRotationY(&Camera_Angle_Matrix_Y, (PI * Camera_Angle.y) / 180);
    267.     D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_Y);
    268.  
    269.     D3DXMatrixRotationX(&Camera_Angle_Matrix_X, (PI * Camera_Angle.x) / 180);
    270.     D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_X);
    271.  
    272.     Device->SetTransform(D3DTS_VIEW, &View_Matrix);
    273. }
    274.  
    275. void Create_Polygon()
    276. {
    277.     Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    278.     Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    279.     Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    280.     Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    281. }
    282.  
    283. void Draw_Polygon()
    284. {
    285.     Device->SetFVF(CUSTOM_VERTEX_FORMAT);
    286.     Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));
    287. }
    288.  
    289. void Create_Platform()
    290. {
    291.     Vertex_List[0] = Create_Custom_Vertex(-1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(255, 0, 0));
    292.     Vertex_List[1] = Create_Custom_Vertex(1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(0, 255, 0));
    293.     Vertex_List[2] = Create_Custom_Vertex(-1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(0, 0, 255));
    294.     Vertex_List[3] = Create_Custom_Vertex(1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(255, 0, 255));
    295. }
    296.  
    297. void Render()
    298. {
    299.     Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    300.     Device->BeginScene();
    301.     //Rendering code goes here
    302.     Create_Polygon();
    303.     Draw_Polygon();
    304.     Create_Platform();
    305.     Draw_Polygon();
    306.     Device->EndScene();
    307.     Device->Present(NULL, NULL, NULL, NULL);
    308. }
    309.  
    310. void Game_Loop()
    311. {
    312.     while (Running == true)
    313.     {
    314.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
    315.         {
    316.             if (WM_QUIT == msg.message) break;
    317.             TranslateMessage (&msg);
    318.             DispatchMessage (&msg);
    319.         }
    320.         else
    321.         {
    322.             Mouse_Controls();
    323.             Keyboard_Controls();
    324.             Camera_Control();
    325.             Render();
    326.             if (DirectInput_Key_State(DIK_ESCAPE)) DestroyWindow(hWnd);
    327.         }
    328.     }
    329. }
    330.  
    331. void Main()
    332. {
    333.     Camera_Position.x = 0.0f;
    334.     Camera_Position.y = -50.0f;
    335.     Camera_Position.z = 250.0f;
    336.  
    337.     DirectX_Initialize();
    338.     DirectInput_Initialize();
    339.     DirectInput_Initialize_Mouse();
    340.     DirectInput_Initialize_Keyboard();
    341.     Settings();
    342.     Setup_Matrices();
    343.     Running = true;
    344. }
    345.  
    346. void Shutdown()
    347. {
    348.     if (Running == true)
    349.     {
    350.         Running = false;
    351.         Mouse_Device->Unacquire();
    352.         Mouse_Device->Release();
    353.         Keyboard_Device->Unacquire();
    354.         Keyboard_Device->Release();
    355.         Device->Release();
    356.         D3D->Release();
    357.         PostQuitMessage (0);
    358.         HANDLE Process;
    359.         Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
    360.         TerminateProcess(Process , 0);
    361.     }
    362. }
    363.  
    364. int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
    365. {
    366.    
    367.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hinstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL};
    368.     RegisterClassEx(&wc);
    369.    
    370.     if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES)
    371.        Fullscreen_Enabled = true;
    372.  
    373.     if (Fullscreen_Enabled == true)
    374.         hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
    375.     else
    376.         hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
    377.     hInstance = hinstance;
    378.     ShowWindow (hWnd, nCmdShow);
    379.  
    380.     if (Fullscreen_Enabled == true)
    381.     {
    382.         Width = 800;
    383.         Height = 600;
    384.     }
    385.     else
    386.     {
    387.         Width = 330;
    388.         Height = 250;
    389.     }
    390.  
    391.     Main();
    392.     Game_Loop();
    393.     return msg.wParam;
    394. }
    395.  
    396. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    397. {
    398.     switch (msg)
    399.     {
    400.         case WM_DESTROY:
    401.             Shutdown();
    402.             break;
    403.         default:
    404.             return DefWindowProc (hWnd, msg, wParam, lParam);
    405.     }
    406.     return 0;
    407. }

    As you can see you can compare the syntax's even though its the exact same program doing the exact same thing. I did my best to maintain variable names, case sensetivity, and function / sub names to keep it consistant.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.