-
Feb 28th, 2012, 02:45 AM
#1
Thread Starter
New Member
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..
-
Feb 28th, 2012, 02:51 AM
#2
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, Photoshop, CodeIgniter, Bootstrap,...
-
Feb 28th, 2012, 03:36 AM
#3
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 03: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.
https://get.cryptobrowser.site/30/4111672
-
Feb 28th, 2012, 08:05 AM
#4
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.
-
Feb 28th, 2012, 08:11 AM
#5
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by cicatrix
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...
-
Feb 28th, 2012, 08:13 AM
#6
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?
-
Feb 28th, 2012, 08:14 AM
#7
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.
-
Feb 28th, 2012, 08:20 AM
#8
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.
-
Feb 28th, 2012, 09:34 AM
#9
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by cicatrix
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.
Originally Posted by love.to.code
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, Photoshop, CodeIgniter, Bootstrap,...
-
Feb 28th, 2012, 10:51 AM
#10
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by akhileshbc
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.
-
Feb 28th, 2012, 11:51 AM
#11
Re: Finally on the verge to decide whether C# or Visual Basic?
My usual boring signature: Nothing
-
Feb 28th, 2012, 12:12 PM
#12
Re: Finally on the verge to decide whether C# or Visual Basic?
!= vs <> is quite frustrating, by the way
-
Feb 28th, 2012, 01:42 PM
#13
Re: Finally on the verge to decide whether C# or Visual Basic?
.Net has a Basic for you:
http://smallbasic.com/
-
Feb 28th, 2012, 05:25 PM
#14
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by cicatrix
!= 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
-
Feb 28th, 2012, 08:40 PM
#15
Member
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 08:52 PM.
~liz
('@ ' )~
-
Feb 28th, 2012, 09:03 PM
#16
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by Nightwalker83
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#.
-
Feb 28th, 2012, 10:54 PM
#17
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by Shaggy Hiker
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.
-
Feb 29th, 2012, 12:33 AM
#18
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by lehgzil
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.
-
Feb 29th, 2012, 03:52 AM
#19
Member
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by cicatrix
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.
Originally Posted by cicatrix
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
-
Feb 29th, 2012, 04:07 AM
#20
Lively Member
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.
-
Feb 29th, 2012, 08:23 AM
#21
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by klen_
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.
-
Feb 29th, 2012, 09:24 AM
#22
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.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Feb 29th, 2012, 10:51 AM
#23
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.
-
Feb 29th, 2012, 11:06 AM
#24
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by penagate
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
-
Feb 29th, 2012, 11:08 AM
#25
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
-
Feb 29th, 2012, 09:38 PM
#26
Member
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by FunkyDexter
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.
Originally Posted by FunkyDexter
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.
-
Feb 29th, 2012, 11:04 PM
#27
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by Shaggy Hiker
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.
-
Feb 29th, 2012, 11:14 PM
#28
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.
-
Mar 1st, 2012, 06:09 AM
#29
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by lehgzil
in c++
#include <stdio.h>
int main()
{
printf("Hello World");
}
Now show us the GUI version (with a window) and MessageBox for C++
-
Mar 1st, 2012, 11:01 AM
#30
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by penagate
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
-
Mar 1st, 2012, 07:45 PM
#31
Member
Re: Finally on the verge to decide whether C# or Visual Basic?
-
Mar 1st, 2012, 08:06 PM
#32
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by cicatrix
!= 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.
https://get.cryptobrowser.site/30/4111672
-
Mar 2nd, 2012, 09:40 AM
#33
Re: Finally on the verge to decide whether C# or Visual Basic?
That's how I feel about Python...
-tg
-
Mar 2nd, 2012, 10:20 AM
#34
Re: Finally on the verge to decide whether C# or Visual Basic?
That's how I feel about Prolog...
-
Mar 3rd, 2012, 09:49 AM
#35
Re: Finally on the verge to decide whether C# or Visual Basic?
That's how I feel about Mandarin.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Mar 3rd, 2012, 12:15 PM
#36
Re: Finally on the verge to decide whether C# or Visual Basic?
Originally Posted by Shaggy Hiker
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.
-
Mar 3rd, 2012, 05:19 PM
#37
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:
Option Explicit Private Type CUSTOM_VERTEX X As Single Y As Single Z As Single Color As Long End Type Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZ Or D3DFVF_DIFFUSE Private Const PI As Single = 3.141592654 Private Const FOV As Single = PI / 4 Private Const ASPECT_RATIO As Single = 3 / 4 Private Const NEAR_Z As Single = 1 Private Const FAR_Z As Single = 10000 Private DX As DirectX8 Private D3D As Direct3D8 Private Device As Direct3DDevice8 Private Display_Mode As D3DDISPLAYMODE Private Screen As D3DPRESENT_PARAMETERS Private DI As DirectInput8 Private Mouse_Device As DirectInputDevice8 Private Mouse_State As DIMOUSESTATE Private Mouse As D3DVECTOR2 Private Keyboard_Device As DirectInputDevice8 Private Keyboard_State As DIKEYBOARDSTATE Private Vertex_List(3) As CUSTOM_VERTEX Private View_Matrix As D3DMATRIX Private Projection_Matrix As D3DMATRIX Private Camera_Position As D3DVECTOR Private Camera_Angle As D3DVECTOR Private Fullscreen_Enabled As Boolean Private Running As Boolean Private Sub DirectX_Initialize() Set DX = New DirectX8 Set D3D = DX.Direct3DCreate() If Fullscreen_Enabled = True Then Display_Mode.Width = 800 Display_Mode.Height = 600 Display_Mode.Format = D3DFMT_R5G6B5 Screen.Windowed = False Screen.BackBufferCount = 1 Screen.BackBufferWidth = Display_Mode.Width Screen.BackBufferHeight = Display_Mode.Height Screen.hDeviceWindow = frmMain.hWnd Else D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode Screen.Windowed = True End If Screen.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC Screen.BackBufferFormat = Display_Mode.Format Screen.AutoDepthStencilFormat = D3DFMT_D16 Screen.EnableAutoDepthStencil = 1 Set Device = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Screen) End Sub Private Sub DirectInput_Initialize() Set DI = DX.DirectInputCreate End Sub Private Sub DirectInput_Initialize_Mouse() Set Mouse_Device = DI.CreateDevice("GUID_SYSMOUSE") Mouse_Device.SetCommonDataFormat DIFORMAT_MOUSE Mouse_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE Mouse_Device.Acquire End Sub Private Sub Mouse_Controls() Mouse_Device.GetDeviceStateMouse Mouse_State Mouse.X = Mouse.X + Mouse_State.lX Mouse.Y = Mouse.Y + Mouse_State.lY If Mouse.X > frmMain.ScaleWidth Then Mouse.X = frmMain.ScaleWidth If Mouse.X < 0 Then Mouse.X = 0 If Mouse.Y > frmMain.ScaleHeight Then Mouse.Y = frmMain.ScaleHeight If Mouse.Y < 0 Then Mouse.Y = 0 Camera_Angle.X = Camera_Angle.X + Mouse_State.lY Camera_Angle.Y = Camera_Angle.Y + Mouse_State.lX End Sub Private Sub DirectInput_Initialize_Keyboard() Set Keyboard_Device = DI.CreateDevice("GUID_SysKeyboard") Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD Keyboard_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE Keyboard_Device.Acquire End Sub Private Function DirectInput_Key_State(Key_Code As Long) As Boolean Keyboard_Device.GetDeviceStateKeyboard Keyboard_State DirectInput_Key_State = Keyboard_State.Key(Key_Code) And &H80 End Function Private Sub Keyboard_Controls() Const Camera_Speed As Long = 5 If DirectInput_Key_State(DIK_W) Then 'Move Forward Camera_Position.X = Camera_Position.X - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_S) Then 'Move Backward Camera_Position.X = Camera_Position.X + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_A) Then 'Move Left Camera_Position.X = Camera_Position.X + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_D) Then 'Move Right Camera_Position.X = Camera_Position.X - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed Camera_Position.Z = Camera_Position.Z + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed End If If DirectInput_Key_State(DIK_Q) Then 'Look Left Camera_Angle.Y = Camera_Angle.Y - 1 End If If DirectInput_Key_State(DIK_E) Then 'Look Right Camera_Angle.Y = Camera_Angle.Y + 1 End If If DirectInput_Key_State(DIK_R) Then 'Look Up Camera_Angle.X = Camera_Angle.X - 1 End If If DirectInput_Key_State(DIK_F) Then 'Look Down Camera_Angle.X = Camera_Angle.X + 1 End If If DirectInput_Key_State(DIK_C) Then 'Move Up Camera_Position.Y = Camera_Position.Y - Camera_Speed End If If DirectInput_Key_State(DIK_Z) Then 'Move Down Camera_Position.Y = Camera_Position.Y + Camera_Speed End If End Sub Private Sub Settings() Device.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE Device.SetRenderState D3DRS_ZWRITEENABLE, 1 Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE Device.SetRenderState D3DRS_LIGHTING, 0 End Sub Private Function Create_Vertex(X As Single, Y As Single, Z As Single) As D3DVECTOR Create_Vertex.X = X Create_Vertex.Y = Y Create_Vertex.Z = Z End Function Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Long) As CUSTOM_VERTEX Create_Custom_Vertex.X = X Create_Custom_Vertex.Y = Y Create_Custom_Vertex.Z = Z Create_Custom_Vertex.Color = Color End Function Private Sub Setup_Matrices() D3DXMatrixLookAtRH View_Matrix, Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0) Device.SetTransform D3DTS_VIEW, View_Matrix D3DXMatrixPerspectiveFovRH Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z Device.SetTransform D3DTS_PROJECTION, Projection_Matrix End Sub Public Sub Camera_Control() Dim Camera_Translation_Matrix As D3DMATRIX Dim Camera_Angle_Matrix_X As D3DMATRIX Dim Camera_Angle_Matrix_Y As D3DMATRIX D3DXMatrixIdentity View_Matrix D3DXMatrixIdentity Camera_Translation_Matrix D3DXMatrixTranslation Camera_Translation_Matrix, Camera_Position.X, Camera_Position.Y, -Camera_Position.Z D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Translation_Matrix D3DXMatrixRotationY Camera_Angle_Matrix_Y, (PI * Camera_Angle.Y) / 180 D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_Y D3DXMatrixRotationX Camera_Angle_Matrix_X, (PI * Camera_Angle.X) / 180 D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_X Device.SetTransform D3DTS_VIEW, View_Matrix End Sub Private Sub Create_Polygon() Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, D3DColorXRGB(255, 255, 255)) Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, D3DColorXRGB(255, 255, 255)) Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, D3DColorXRGB(255, 255, 255)) Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, D3DColorXRGB(255, 255, 255)) End Sub Private Sub Draw_Polygon() Device.SetVertexShader CUSTOM_VERTEX_FORMAT Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0)) End Sub Private Sub Create_Platform() Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, D3DColorXRGB(255, 0, 0)) Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, D3DColorXRGB(0, 255, 0)) Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, D3DColorXRGB(0, 0, 255)) Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, D3DColorXRGB(255, 0, 255)) End Sub Private Sub Render() Device.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, D3DColorXRGB(0, 0, 0), 1, 0 Device.BeginScene 'Rendering code goes here Create_Platform Draw_Polygon Create_Polygon Draw_Polygon Device.EndScene Device.Present ByVal 0, ByVal 0, 0, ByVal 0 End Sub Private Sub Game_Loop() Do While Running = True Mouse_Controls Keyboard_Controls Camera_Control Render If DirectInput_Key_State(DIK_ESCAPE) <> 0 Then Shutdown DoEvents Loop End Sub Private Sub Main() If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True End If With Me .Show .ScaleMode = vbPixels .ScaleWidth = 312 .ScaleHeight = 213 .Caption = "DirectX Tutorial" If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone End With Camera_Position.X = 0 Camera_Position.Y = -50 Camera_Position.Z = 250 DirectX_Initialize DirectInput_Initialize DirectInput_Initialize_Mouse DirectInput_Initialize_Keyboard Settings Setup_Matrices Running = True Game_Loop End Sub Private Sub Shutdown() If Running = True Then Running = False Mouse_Device.Unacquire Set Mouse_Device = Nothing Keyboard_Device.Unacquire Set Keyboard_Device = Nothing Set DI = Nothing Set Device = Nothing Set D3D = Nothing Set DX = Nothing Unload Me End If End Sub Private Sub Form_Load() Main End Sub Private Sub Form_Unload(Cancel As Integer) Shutdown End Sub
CONTINUED NEXT POST...
-
Mar 3rd, 2012, 05:20 PM
#38
Re: Finally on the verge to decide whether C# or Visual Basic?
CONTINUED FROM LAST POST...
VB.NET - DX9
vb.net Code:
Option Explicit On Option Strict On Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Imports Microsoft.DirectX.DirectInput Public Class frmMain Private Const FOV As Single = Math.PI / 4 Private Const ASPECT_RATIO As Single = 4 / 3 Private Const NEAR_Z As Single = 1 Private Const FAR_Z As Single = 10000 Private Device As Direct3D.Device Private Display_Mode As DisplayMode Private Screen As New PresentParameters Private Mouse_Device As DirectInput.Device Private Mouse_State As MouseState Private Mouse As Vector2 Private Keyboard_Device As DirectInput.Device Private Keyboard_State As KeyboardState Private Vertex_List(3) As CustomVertex.PositionColored Private View_Matrix As Matrix Private Projection_Matrix As Matrix Private Camera_Position As Vector3 Private Camera_Angle As Vector3 Private Fullscreen_Enabled As Boolean Private Running As Boolean Private Sub DirectX_Initialize() If Fullscreen_Enabled = True Then Display_Mode.Width = 800 Display_Mode.Height = 600 Display_Mode.Format = Direct3D.Format.R5G6B5 Screen.Windowed = False Screen.BackBufferCount = 1 Screen.BackBufferWidth = Display_Mode.Width Screen.BackBufferHeight = Display_Mode.Height Else Screen.Windowed = True End If Screen.SwapEffect = SwapEffect.Copy Screen.BackBufferFormat = Display_Mode.Format Screen.AutoDepthStencilFormat = DepthFormat.D16 Screen.EnableAutoDepthStencil = True Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen) End Sub Private Sub DirectInput_Initialize_Mouse() Mouse_Device = New DirectInput.Device(SystemGuid.Mouse) Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse) Mouse_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive) Mouse_Device.Acquire() End Sub Private Sub Mouse_Controls() Mouse_State = Mouse_Device.CurrentMouseState Mouse.X = Mouse.X + Mouse_State.X Mouse.Y = Mouse.Y + Mouse_State.Y If Mouse.X > Me.Width Then Mouse.X = Me.Width If Mouse.X < 0 Then Mouse.X = 0 If Mouse.Y > Me.Height Then Mouse.Y = Me.Height If Mouse.Y < 0 Then Mouse.Y = 0 Camera_Angle.X += Mouse_State.Y Camera_Angle.Y += Mouse_State.X If Camera_Angle.X >= 90 Then Camera_Angle.X = 90 If Camera_Angle.X <= -90 Then Camera_Angle.X = -90 End Sub Private Sub DirectInput_Initialize_Keyboard() Keyboard_Device = New DirectInput.Device(SystemGuid.Keyboard) Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard) Keyboard_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive) Keyboard_Device.Acquire() End Sub Private Function DirectInput_Key_State(ByVal Key_Code As DirectInput.Key) As Boolean Keyboard_State = Keyboard_Device.GetCurrentKeyboardState Return Keyboard_State.Item(Key_Code) End Function Private Sub Keyboard_Controls() Const Camera_Speed As Long = 5 If DirectInput_Key_State(Key.W) Then 'Move Forward Camera_Position.X -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.S) Then 'Move Backward Camera_Position.X += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.A) Then 'Move Left Camera_Position.X += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.D) Then 'Move Right Camera_Position.X -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed Camera_Position.Z += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed End If If DirectInput_Key_State(Key.Q) Then 'Look Left Camera_Angle.Y -= 1 End If If DirectInput_Key_State(Key.E) Then 'Look Right Camera_Angle.Y += 1 End If If DirectInput_Key_State(Key.R) Then 'Look Up Camera_Angle.X -= 1 End If If DirectInput_Key_State(Key.F) Then 'Look Down Camera_Angle.X += 1 End If If DirectInput_Key_State(Key.C) Then 'Move Up Camera_Position.Y -= Camera_Speed End If If DirectInput_Key_State(Key.Z) Then 'Move Down Camera_Position.Y += Camera_Speed End If End Sub Private Sub Settings() Device.SetRenderState(RenderStates.ZEnable, True) Device.SetRenderState(RenderStates.ZBufferWriteEnable, True) Device.SetRenderState(RenderStates.CullMode, Cull.None) Device.SetRenderState(RenderStates.Lighting, False) End Sub Private Function Create_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Vector3 Dim Vertex As Vector3 Vertex.X = X Vertex.Y = Y Vertex.Z = Z Return Vertex End Function Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer) As CustomVertex.PositionColored Dim Vertex As CustomVertex.PositionColored = New CustomVertex.PositionColored Vertex.Position = New Vector3(X, Y, Z) Vertex.Color = Color Return Vertex End Function Private Sub Setup_Matrices() 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)) Device.Transform.View = View_Matrix Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z) Device.Transform.Projection = Projection_Matrix End Sub Private Sub Camera_Control() Dim Camera_Translation_Matrix As Matrix Dim Camera_Angle_Matrix_X As Matrix Dim Camera_Angle_Matrix_Y As Matrix View_Matrix = Matrix.Identity Camera_Translation_Matrix = Matrix.Identity Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z) View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix) Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180) View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y) Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180) View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X) Device.Transform.View = View_Matrix End Sub Private Sub Create_Polygon() Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb) Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb) Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb) Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb) End Sub Private Sub Draw_Polygon() Device.VertexFormat = CustomVertex.PositionColored.Format Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List) End Sub Private Sub Create_Platform() Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb) Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb) Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb) Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb) End Sub Private Sub Reset_Device() If Me.WindowState <> FormWindowState.Minimized And Fullscreen_Enabled = False Then Running = False Device.Reset(Screen) Settings() Setup_Matrices() Application.DoEvents() Running = True End If End Sub Private Sub Render() Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1, 0) Device.BeginScene() Create_Polygon() Draw_Polygon() Create_Platform() Draw_Polygon() Device.EndScene() Device.Present() End Sub Private Sub Game_Loop() Do While Running = True Mouse_Controls() Keyboard_Controls() Camera_Control() Render() If DirectInput_Key_State(Key.Escape) Then Shutdown() Application.DoEvents() Loop End Sub Private Sub Main() If MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then Fullscreen_Enabled = True End If With Me .Show() .Width = 330 .Height = 250 .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) .Text = "DirectX Tutorial" If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None End With Camera_Position.X = 0 Camera_Position.Y = -50 Camera_Position.Z = 250 DirectX_Initialize() DirectInput_Initialize_Mouse() DirectInput_Initialize_Keyboard() Settings() Setup_Matrices() Running = True End Sub Private Sub Shutdown() If Running = True Then Running = False Mouse_Device.Unacquire() Mouse_Device.Dispose() Keyboard_Device.Unacquire() Keyboard_Device.Dispose() Device.Dispose() Application.Exit() End If End Sub Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Main() End Sub Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Shutdown() End Sub Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Game_Loop() End Sub Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize Reset_Device() End Sub End Class
CONTINUED NEXT POST...
-
Mar 3rd, 2012, 05:21 PM
#39
Re: Finally on the verge to decide whether C# or Visual Basic?
CONTINUED FROM LAST POST...
C# - DX9
c# Code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; using Microsoft.DirectX.DirectInput; namespace DX_Tut { public partial class frmMain : Form { private const float FOV = (float)Math.PI / 4.0f; private const float ASPECT_RATIO = 4.0f / 3.0f; private const float NEAR_Z = 1.0f; private const float FAR_Z = 10000.0f; Microsoft.DirectX.Direct3D.Device Device; DisplayMode Display_Mode; PresentParameters Screen; Microsoft.DirectX.DirectInput.Device Mouse_Device; MouseState Mouse_State; Vector2 Mouse; Microsoft.DirectX.DirectInput.Device Keyboard_Device; KeyboardState Keyboard_State; CustomVertex.PositionColored[] Vertex_List = new CustomVertex.PositionColored[4]; Matrix View_Matrix; Matrix Projection_Matrix; Vector3 Camera_Position; Vector3 Camera_Angle; bool Fullscreen_Enabled; bool Running; private void DirectX_Initialize() { Screen = new PresentParameters(); if (Fullscreen_Enabled == true) { Display_Mode.Width = 800; Display_Mode.Height = 600; Display_Mode.Format = Format.R5G6B5; Screen.Windowed = false; Screen.BackBufferCount = 1; Screen.BackBufferWidth = Display_Mode.Width; Screen.BackBufferHeight = Display_Mode.Height; } else { Screen.Windowed = true; } Screen.SwapEffect = SwapEffect.Copy; Screen.BackBufferFormat = Display_Mode.Format; Screen.AutoDepthStencilFormat = DepthFormat.D16; Screen.EnableAutoDepthStencil = true; Device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen); } private void DirectInput_Initialize_Mouse() { Mouse_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse); Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse); Mouse_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); Mouse_Device.Acquire(); } private void Mouse_Controls() { Mouse_State = Mouse_Device.CurrentMouseState; Mouse.X += Mouse_State.X; Mouse.Y += Mouse_State.Y; if (Mouse.X > this.Width) Mouse.X = this.Width; if (Mouse.X < 0) Mouse.X = 0; if (Mouse.Y > this.Height) Mouse.Y = this.Height; if (Mouse.Y < 0) Mouse.Y = 0; Camera_Angle.X += Mouse_State.Y; Camera_Angle.Y += Mouse_State.X; if (Camera_Angle.X >= 90) Camera_Angle.X = 90; if (Camera_Angle.X <= -90) Camera_Angle.X = -90; } private void DirectInput_Initialize_Keyboard() { Keyboard_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard); Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard); Keyboard_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); Keyboard_Device.Acquire(); } private bool DirectInput_Key_State(Microsoft.DirectX.DirectInput.Key Key_Code) { Keyboard_State = Keyboard_Device.GetCurrentKeyboardState(); return Keyboard_State[Key_Code]; } private void Keyboard_Controls() { const int Camera_Speed = 5; if (DirectInput_Key_State(Key.W)) //Move Forward { Camera_Position.X -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.S)) //Move Backward { Camera_Position.X += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.A)) //Move Left { Camera_Position.X += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.D)) //Move Right { Camera_Position.X -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; Camera_Position.Z += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed; } if (DirectInput_Key_State(Key.Q)) //Look Left Camera_Angle.Y -= 1; if (DirectInput_Key_State(Key.E)) //Look Right Camera_Angle.Y += 1; if (DirectInput_Key_State(Key.R)) //Look Up Camera_Angle.X -= 1; if (DirectInput_Key_State(Key.F)) //Look Down Camera_Angle.X += 1; if (DirectInput_Key_State(Key.C)) //Move Up Camera_Position.Y -= Camera_Speed; if (DirectInput_Key_State(Key.Z)) //Move Down Camera_Position.Y += Camera_Speed; } private void Settings() { Device.SetRenderState(RenderStates.ZEnable, true); Device.SetRenderState(RenderStates.ZBufferWriteEnable, true); Device.SetRenderState(RenderStates.CullMode, (int)Cull.None); Device.SetRenderState(RenderStates.Lighting, false); } private Vector3 Create_Vertex(float X, float Y, float Z) { Vector3 Vertex; Vertex.X = X; Vertex.Y = Y; Vertex.Z = Z; return Vertex; } private CustomVertex.PositionColored Create_Custom_Vertex(float X, float Y, float Z, int Color) { CustomVertex.PositionColored Vertex = new CustomVertex.PositionColored(); Vertex.Position = new Vector3(X, Y, Z); Vertex.Color = Color; return Vertex; } private void Setup_Matrices() { 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)); Device.Transform.View = View_Matrix; Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z); Device.Transform.Projection = Projection_Matrix; } private void Camera_Control() { Matrix Camera_Translation_Matrix; Matrix Camera_Angle_Matrix_X; Matrix Camera_Angle_Matrix_Y; View_Matrix = Matrix.Identity; Camera_Translation_Matrix = Matrix.Identity; Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z); View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix); Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180); View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y); Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180); View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X); Device.Transform.View = View_Matrix; } private void Create_Polygon() { Vertex_List[0] = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb()); Vertex_List[1] = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb()); Vertex_List[2] = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb()); Vertex_List[3] = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb()); } private void Draw_Polygon() { Device.VertexFormat = CustomVertex.PositionColored.Format; Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List); } private void Create_Platform() { Vertex_List[0] = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb()); Vertex_List[1] = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb()); Vertex_List[2] = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb()); Vertex_List[3] = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb()); } private void Reset_Device() { if (this.WindowState != FormWindowState.Minimized && Fullscreen_Enabled == false) { Running = false; Device.Reset(Screen); Settings(); Setup_Matrices(); Application.DoEvents(); Running = true; } } private void Render() { Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1.0f, 0); Device.BeginScene(); Create_Platform(); Draw_Polygon(); Create_Polygon(); Draw_Polygon(); Device.EndScene(); Device.Present(); } private void Game_Loop() { do { Mouse_Controls(); Keyboard_Controls(); Camera_Control(); Render(); if (DirectInput_Key_State(Key.Escape)) Shutdown(); Application.DoEvents(); } while (Running == true); } private void Main() { if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) Fullscreen_Enabled = true; this.Show(); this.KeyPreview = true; this.Width = 330; this.Height = 250; this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); this.Text = "DirectX Tutorial"; if (Fullscreen_Enabled == true) this.FormBorderStyle = FormBorderStyle.None; Camera_Position.X = 0; Camera_Position.Y = -50; Camera_Position.Z = 250; DirectX_Initialize(); DirectInput_Initialize_Mouse(); DirectInput_Initialize_Keyboard(); Settings(); Setup_Matrices(); Running = true; } void Shutdown() { if (Running == true) { Running = false; Mouse_Device.Unacquire(); Mouse_Device.Dispose(); Keyboard_Device.Unacquire(); Keyboard_Device.Dispose(); Device.Dispose(); Application.Exit(); } } public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { Main(); } private void frmMain_Paint(object sender, PaintEventArgs e) { Game_Loop(); } private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { Shutdown(); } private void frmMain_Resize(object sender, EventArgs e) { Reset_Device(); } } }
CONTINUED NEXT POST...
-
Mar 3rd, 2012, 05:23 PM
#40
Re: Finally on the verge to decide whether C# or Visual Basic?
CONTINUED FROM LAST POST...
C++ - DX9
c++ Code:
#include <windows.h> #include <d3d9.h> #include <d3dx9.h> #include <dinput.h> #pragma comment (lib, "d3d9.lib") #pragma comment (lib, "d3dx9.lib") #pragma comment (lib, "dinput8.lib") #pragma comment (lib, "dxguid.lib") struct CUSTOM_VERTEX { float X, Y, Z; DWORD Color; }; #define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE) const float PI = 3.141592654f; const float FOV = PI / 4.0f; const float ASPECT_RATIO = 4.0f / 3.0f; const float NEAR_Z = 1.0f; const float FAR_Z = 10000.0f; HWND hWnd; MSG msg; HINSTANCE hInstance; int Width; int Height; LPDIRECT3D9 D3D; LPDIRECT3DDEVICE9 Device; D3DDISPLAYMODE Display_Mode; D3DPRESENT_PARAMETERS Screen; LPDIRECTINPUT8 DI; LPDIRECTINPUTDEVICE8 Mouse_Device; DIMOUSESTATE Mouse_State; D3DXVECTOR2 Mouse; LPDIRECTINPUTDEVICE8 Keyboard_Device; BYTE Keyboard_State[256]; CUSTOM_VERTEX Vertex_List[4]; D3DXMATRIX View_Matrix; D3DXMATRIX Projection_Matrix; D3DVECTOR Camera_Position; D3DVECTOR Camera_Angle; bool Fullscreen_Enabled; bool Running; void DirectX_Initialize(void); void DirectInput_Initialize(void); void DirectInput_Initialize_Mouse(void); void Mouse_Controls(void); void DirectInput_Initialize_Keyboard(void); bool DirectInput_Key_State(void); void Keyboard_Controls(void); void Settings(void); D3DXVECTOR3 Create_Vertex(void); CUSTOM_VERTEX Create_Custom_Vertex(void); void Setup_Matrices(void); void Camera_Control(void); void Create_Polygon(void); void Draw_Polygon(void); void Create_Platform(void); void Render(void); void Game_Loop(void); void Main(void); void Shutdown(void); LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void DirectX_Initialize() { D3D = Direct3DCreate9(D3D_SDK_VERSION); memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS)); if (Fullscreen_Enabled == true) { Display_Mode.Width = 800; Display_Mode.Height = 600; Display_Mode.Format = D3DFMT_R5G6B5; Screen.Windowed = FALSE; Screen.BackBufferCount = 1; Screen.BackBufferWidth = Display_Mode.Width; Screen.BackBufferHeight = Display_Mode.Height; Screen.hDeviceWindow = hWnd; } else { D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode); Screen.Windowed = TRUE; } Screen.SwapEffect = D3DSWAPEFFECT_DISCARD; Screen.BackBufferFormat = D3DFMT_R5G6B5; Screen.EnableAutoDepthStencil = TRUE; Screen.AutoDepthStencilFormat = D3DFMT_D16; D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device); } void DirectInput_Initialize() { DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL); } void DirectInput_Initialize_Mouse() { DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL); Mouse_Device->SetDataFormat(&c_dfDIMouse); Mouse_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); Mouse_Device->Acquire(); } void Mouse_Controls() { Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State); Mouse.x += Mouse_State.lX; Mouse.y += Mouse_State.lY; if (Mouse.x > Width) Mouse.x = (float)Width; if (Mouse.y < 0) Mouse.x = 0; if (Mouse.x > Height) Mouse.y = (float)Height; if (Mouse.y < 0) Mouse.y = 0; Camera_Angle.x += Mouse_State.lY; Camera_Angle.y += Mouse_State.lX; if (Camera_Angle.x >= 90) Camera_Angle.x = 90; if (Camera_Angle.x <= -90) Camera_Angle.x = -90; } void DirectInput_Initialize_Keyboard() { DI->CreateDevice(GUID_SysKeyboard, &Keyboard_Device, NULL); Keyboard_Device->SetDataFormat(&c_dfDIKeyboard); Keyboard_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE); Keyboard_Device->Acquire(); } bool DirectInput_Key_State(int Key_Code) { Keyboard_Device->GetDeviceState(256, (LPVOID)Keyboard_State); return Keyboard_State[Key_Code] && 0x80; } void Keyboard_Controls() { const int Camera_Speed = 5; if (DirectInput_Key_State(DIK_W)) { Camera_Position.x -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_S)) { Camera_Position.x += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_A)) { Camera_Position.x += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_D)) { Camera_Position.x -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed; Camera_Position.z += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed; } if (DirectInput_Key_State(DIK_Q)) { Camera_Angle.y -= 1; } if (DirectInput_Key_State(DIK_E)) { Camera_Angle.y += 1; } if (DirectInput_Key_State(DIK_R)) { Camera_Angle.x -= 1; } if (DirectInput_Key_State(DIK_F)) { Camera_Angle.x += 1; } if (DirectInput_Key_State(DIK_C)) { Camera_Position.y -= Camera_Speed; } if (DirectInput_Key_State(DIK_Z)) { Camera_Position.y += Camera_Speed; } } void Settings() { Device->SetRenderState(D3DRS_ZENABLE, TRUE); Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); Device->SetRenderState(D3DRS_LIGHTING, FALSE); } D3DXVECTOR3 Create_Vertex(float X, float Y, float Z) { D3DXVECTOR3 Vertex; Vertex.x = X; Vertex.y = Y; Vertex.z = Z; return Vertex; } CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, DWORD Color) { CUSTOM_VERTEX Vertex; Vertex.X = X; Vertex.Y = Y; Vertex.Z = Z; Vertex.Color = Color; return Vertex; } void Setup_Matrices() { 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)); Device->SetTransform(D3DTS_VIEW, &View_Matrix); D3DXMatrixPerspectiveFovRH(&Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z); Device->SetTransform(D3DTS_PROJECTION, &Projection_Matrix); } void Camera_Control() { D3DXMATRIX Camera_Translation_Matrix; D3DXMATRIX Camera_Angle_Matrix_X; D3DXMATRIX Camera_Angle_Matrix_Y; D3DXMatrixIdentity(&View_Matrix); D3DXMatrixIdentity(&Camera_Translation_Matrix); D3DXMatrixTranslation(&Camera_Translation_Matrix, Camera_Position.x, Camera_Position.y, -Camera_Position.z); D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Translation_Matrix); D3DXMatrixRotationY(&Camera_Angle_Matrix_Y, (PI * Camera_Angle.y) / 180); D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_Y); D3DXMatrixRotationX(&Camera_Angle_Matrix_X, (PI * Camera_Angle.x) / 180); D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_X); Device->SetTransform(D3DTS_VIEW, &View_Matrix); } void Create_Polygon() { Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255)); } void Draw_Polygon() { Device->SetFVF(CUSTOM_VERTEX_FORMAT); Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX)); } void Create_Platform() { Vertex_List[0] = Create_Custom_Vertex(-1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(255, 0, 0)); Vertex_List[1] = Create_Custom_Vertex(1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(0, 255, 0)); Vertex_List[2] = Create_Custom_Vertex(-1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(0, 0, 255)); Vertex_List[3] = Create_Custom_Vertex(1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(255, 0, 255)); } void Render() { Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); Device->BeginScene(); //Rendering code goes here Create_Polygon(); Draw_Polygon(); Create_Platform(); Draw_Polygon(); Device->EndScene(); Device->Present(NULL, NULL, NULL, NULL); } void Game_Loop() { while (Running == true) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0) { if (WM_QUIT == msg.message) break; TranslateMessage (&msg); DispatchMessage (&msg); } else { Mouse_Controls(); Keyboard_Controls(); Camera_Control(); Render(); if (DirectInput_Key_State(DIK_ESCAPE)) DestroyWindow(hWnd); } } } void Main() { Camera_Position.x = 0.0f; Camera_Position.y = -50.0f; Camera_Position.z = 250.0f; DirectX_Initialize(); DirectInput_Initialize(); DirectInput_Initialize_Mouse(); DirectInput_Initialize_Keyboard(); Settings(); Setup_Matrices(); Running = true; } void Shutdown() { if (Running == true) { Running = false; Mouse_Device->Unacquire(); Mouse_Device->Release(); Keyboard_Device->Unacquire(); Keyboard_Device->Release(); Device->Release(); D3D->Release(); PostQuitMessage (0); HANDLE Process; Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId()); TerminateProcess(Process , 0); } } int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hinstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL}; RegisterClassEx(&wc); if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES) Fullscreen_Enabled = true; if (Fullscreen_Enabled == true) 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); else hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL); hInstance = hinstance; ShowWindow (hWnd, nCmdShow); if (Fullscreen_Enabled == true) { Width = 800; Height = 600; } else { Width = 330; Height = 250; } Main(); Game_Loop(); return msg.wParam; } LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: Shutdown(); break; default: return DefWindowProc (hWnd, msg, wParam, lParam); } return 0; }
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|