• What do you need help with? V. 3.0
    4,884 replies, posted
[QUOTE=Lord Ned;30144977]Can C# save/load a struct to file? I know it's possible in C++, I think I did fopen on the file, casted the pointer as a byte and then casted it as my struct and that filled the struct for me. However, I don't see a simple way to do it in C# - Maybe I'm just missing the function? [editline]30th May 2011[/editline] Here's how I did it in C++ [code] studiohdr_t *StudioModel::LoadModel(char *modelname) { //If they didn't specify a modelname, return 0 if (!modelname) return 0; //Load the File as a pointer FILE *pFile; long size; void *buffer; if( (pFile = fopen( modelname, "rb" )) == NULL) { //Couldn't read the file return 0; } //Find the end of the file fseek( pFile, 0, SEEK_END ); //Set the size as the end (Number of Bytes in the file) size = ftell( pFile ); //Find the start of the file again fseek( pFile, 0, SEEK_SET ); //Set the buffer to the memory allocated for the model buffer = malloc( size ); if (!buffer) { //Couldn't allocate the memory for it fclose( pFile ); //Close the file return 0; } //Read the entire file (buffer = location in memory, //size = how much memory to read, 1 = how many 'blocks' of that //much to read, and pFile = file to read fread( buffer, size, 1, pFile ); fclose( pFile ); //Close the file byte *pin; studiohdr_t *phdr; mstudiotexture_t *ptexture; char *pCdtexture2; //Version Header/ID pin = (byte *)buffer; //Cast the Version Header/ID as a studioheader struct. phdr = (studiohdr_t *)pin; //Texture ptexture = (mstudiotexture_t *) (pin + phdr->textureindex ); pCdtexture2 = phdr->pCdtexture( 0 ); if (strncmp( (const char*) buffer, "IDST", 4) && strncmp( (const char*) buffer, "IDSQ", 4)) { //ToDo: IDSQ is probably only a HL1 header. Don't need support for it. //See studio_utils.cpp line 166 from real renderer //strncmp //A value greater than zero indicates that the first character that does not //match has a greater value in str1 than in str2; And a value less than zero //indicates the opposite. //Wrong File Header! MessageBoxA( NULL, "Wrong file header!", "MDL Loader", MB_OK ); free( buffer ); return 0; } if (phdr->textureindex > 0 && phdr->numtextures <= MAXSTUDIOSKINS ) { int n = phdr->numtextures; for ( int i = 0; i < n; i++) { if (!m_ptexturecd[i]) m_ptexturecd[i] = phdr->pCdtexture( i ); } } if (!m_pstudiohdr) m_pstudiohdr = (studiohdr_t *) buffer; if (!m_ptexturehdr) m_ptexturehdr = (mstudiotexture_t *) (pin + phdr->textureindex ); return (studiohdr_t *)buffer; }[/code][/QUOTE] Save the struct: [code] XmlSerializer writer = new XmlSerializer(mystruct.GetType()); StreamWriter file = new StreamWriter("filename.xml"); writer.Serialize(file, mystruct); file.Close(); [/code] Load the struct: [code] XmlSerializer reader= new XmlSerializer(mystruct.GetType()); StreamWriter file = new StreamWriter("filename.xml"); return reader.DeSerialize(file, mystruct); file.Close(); [/code]
[QUOTE=MadPro119;30145813]Are you sure? Like this? That outputs an error. I'm probly just and idiot. [editline]30th May 2011[/editline] Or is that the wrong else?[/QUOTE] [csharp] if (hacc >= 20) //I should add the monsters damage in here too. { mhp = mhp - hdmg; Console.WriteLine("Your attack hits the enemy. The monster has " + mhp + " health remaining"); Random mdmgr; mdmgr = new Random(); mdmg = mdmgr.Next(1, 25)- hdmg; hhp = hhp - mdmg; switch (hdmg) { case (hdmg>=1 && hdmg <=4): hhp = hhp - mdmg; hhp = hhp - 5; Console.WriteLine("Your weak attack barely hurts the monster who strikes back with ease for " +(mdmg+5)+" damage"); break; case (hdmg>=5 && hdmg <=16): Random maccr; maccr = new Random(); macc = maccr.Next (1, 100); if (macc >= 40) { hhp = hhp - mdmg; Console.WriteLine("The monster powers on attacking you for " +mdmg+ " health."); } else { Console.WriteLine("The monster misses it's attack!"); } break; case (hdmg>=17): Console.WriteLine("Your attack is so powerful the enemy is unable to counter it!"); break; } else { [/csharp] Well there the first if never ends Also you probably have an extra } after that else
[QUOTE=Richy19;30145842]Is there meant to be a question in there?[/QUOTE] The question is: How do i create XNB files from images or xml files using MSBuild?
[QUOTE=Lord Ned;30144977]Can C# save/load a struct to file? I know it's possible in C++, I think I did fopen on the file, casted the pointer as a byte and then casted it as my struct and that filled the struct for me. However, I don't see a simple way to do it in C# - Maybe I'm just missing the function?[/QUOTE] Reference the following namespaces: System.Runtime.Serialization and System.Runtime.Serialization.Formatters.Binary then do this: First mark your struct as Serializable (Put a [Serializable] before the struct declaration), then you need to implement the ISerializeable interface and add a new constructor: [csharp] public Something(SerializationInfo info, StreamingContext context) { Name = info.GetString("Name"); Id = info.GetInt32("Id"); } //ISerializeable member public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", Name); info.AddValue("Id", Id); }[/csharp] Then to serialize you can use a BinaryFormatter, here's some handy functions: [csharp] public static void Serialize(string destFilePath, ISerializable obj) { using (Stream fstream = File.Open(destFilePath, FileMode.Create)) { BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(fstream, obj); fstream.Close(); } } public static T UnSerialize<T>(string filePath) where T : ISerializable { using (Stream fstream = File.Open(filePath, FileMode.Open)) { BinaryFormatter bformat = new BinaryFormatter(); T obj = (T)bformat.Deserialize(fstream); fstream.Close(); return obj; } }[/csharp]
[QUOTE=Kukks;30145770]I'm working on an XNA game library and I have absolutely no idea how I'm going to compile content to the XNB Format. Basically I'm making a tool to convert images & xml to xnb.[/QUOTE] Why's that? If you import it into the Content part of your project, they'll be compiled into xnb assets on compile. That being said, there's an example on MSDN about loading content (Mostly models) in real time - It compiles it and saves it temporarily. Fox-Face that makes no sense without any formatting. Kukks: XML :( I'm probably going to be storing a lot of floats, so I'd want a binary format, no? (Binary just to save time/space, not specifically cause of lots of floats)
[QUOTE=Richy19;30145883][csharp] if (hacc >= 20) //I should add the monsters damage in here too. { mhp = mhp - hdmg; Console.WriteLine("Your attack hits the enemy. The monster has " + mhp + " health remaining"); Random mdmgr; mdmgr = new Random(); mdmg = mdmgr.Next(1, 25)- hdmg; hhp = hhp - mdmg; switch (hdmg) { case (hdmg>=1 && hdmg <=4): hhp = hhp - mdmg; hhp = hhp - 5; Console.WriteLine("Your weak attack barely hurts the monster who strikes back with ease for " +(mdmg+5)+" damage"); break; case (hdmg>=5 && hdmg <=16): Random maccr; maccr = new Random(); macc = maccr.Next (1, 100); if (macc >= 40) { hhp = hhp - mdmg; Console.WriteLine("The monster powers on attacking you for " +mdmg+ " health."); } else { Console.WriteLine("The monster misses it's attack!"); } break; case (hdmg>=17): Console.WriteLine("Your attack is so powerful the enemy is unable to counter it!"); break; } else { [/csharp] Well there the first if never ends Also you probably have an extra } after that else[/QUOTE] What you said there really didnt make sense to me. Although I replaced my code with yours and it didnt fix anything.
[QUOTE=Lord Ned;30146056]Fox-Face that makes no sense without any formatting.[/QUOTE] Yea I broke the code box by mistake :v: should be good now.
[QUOTE=Fox-Face;30146104]Yea I broke the code box by mistake :v: should be good now.[/QUOTE] So you Sereialize data, and then write the sereialized data to file, then to load you load serealized data, and unserealize it? I'm not quite following what I have to do here. Based on "then you need to implement the ISerializeable interface and add a new constructor", I want to make a new class that extensd ISerealizeable with that constructor in it, and then make a new instance of the class to serealize my data?
[QUOTE=Lord Ned;30146056]Why's that? If you import it into the Content part of your project, they'll be compiled into xnb assets on compile. That being said, there's an example on MSDN about loading content (Mostly models) in real time - It compiles it and saves it temporarily. Fox-Face that makes no sense without any formatting. Kukks: XML :( I'm probably going to be storing a lot of floats, so I'd want a binary format, no? (Binary just to save time/space, not specifically cause of lots of floats)[/QUOTE] I didn't understand that example all that well and basically I'm aiming for a flexible dynamic library that doesn't require you to add content beforehand.. Regarding your prob I've never had any performance issues with XML and I have no idea how to automatically serialize to a binary format so sorry :/
[QUOTE=Lord Ned;30146207]So you Sereialize data, and then write the sereialized data to file, then to load you load serealized data, and unserealize it? I'm not quite following what I have to do here. Based on "then you need to implement the ISerializeable interface and add a new constructor", I want to make a new class that extensd ISerealizeable with that constructor in it, and then make a new instance of the class to serealize my data?[/QUOTE] Let me post a quick example, hang on Here: [csharp][Serializable] public struct Something : ISerializable { public string Name; public int Id; public Something(string Name, int ID) { this.Name = Name; this.Id = ID; } public Something(SerializationInfo info, StreamingContext context) { Name = info.GetString("Name"); Id = info.GetInt32("Id"); } //ISerializeable member public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", Name); info.AddValue("Id", Id); } } class Program { static void Main(string[] args) { Something s = new Something("Bla", 1); Serializer.Serialize("test", s); Something s2 = Serializer.UnSerialize<Something>("test"); Console.WriteLine(s2.Name); } } public class Serializer { public static void Serialize(string destFilePath, ISerializable obj) { using (Stream fstream = File.Open(destFilePath, FileMode.Create)) { BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(fstream, obj); fstream.Close(); } } public static T UnSerialize<T>(string filePath) where T : ISerializable { using (Stream fstream = File.Open(filePath, FileMode.Open)) { BinaryFormatter bformat = new BinaryFormatter(); T obj = (T)bformat.Deserialize(fstream); fstream.Close(); return obj; } } }[/csharp] You need to implement ISerializeable in your struct, which will contan only the GetObjectData method, this will be used by the BinaryFormatter to serialize your struct, and the constructor will be used by it to unserialize, then you just pass the object to it.
[QUOTE=Kukks;30146208]I didn't understand that example all that well and basically I'm aiming for a flexible dynamic library that doesn't require you to add content beforehand..[/QUOTE] Let me know how this works out. You can load Texture's at run time, and it's possible to load ASCII *FBX files. However you'll have to write your own importer. See here for an overview: [url]http://thunderfist-podium.blogspot.com/2008/09/xna-fbx-and-custom-loader.html[/url] That's really the biggest flaw I see with XNA right now. Makes modding very hard. [QUOTE=Fox-Face;30146254]Let me post a quick example, hang on [/quote] Oh god I would have done it terribly, terribly wrong. :( Thanks for that, I'll go implement it after I get rendering up and running again.
[QUOTE=MadPro119;30146065]What you said there really didnt make sense to me. Although I replaced my code with yours and it didnt fix anything.[/QUOTE] because i didnt change anything try this [code]using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int hhp = 100; //I'll just declare all (most) my variables here for safe keeping int mhp = 100; int mhp2 = 100; int mhp3 = 100; int hdmg; int mdmg; int nom; int fight; int name; int hacc; int macc; int def; string choice; string mname = "CRAP"; A:Console.WriteLine("Text Based Battle System Tech Demo"); //intro text Console.ReadLine(); Random nomr; // How many monster will you fight, 1 - 3. nomr = new Random(); nom = nomr.Next(1, 4); Random namer; //Random Name namer = new Random(); name = namer.Next(1, 4); if (nom == 1) //Random Naming { switch (name) { case 1: mname = "Hogworm"; break; case 2: mname = "Boar"; break; case 3: mname = "Death Claw"; break; } } else { switch (name) { case 1: mname = "Hogworms"; break; case 2: mname = "Boars"; break; case 3: mname = "Death Claws"; break; } } Random fightr; //If you find a fight fightr = new Random(); fight = fightr.Next(1, 10); if (fight >= 1) { Console.WriteLine("You encounter " + nom + " wild " + mname + "."); Console.ReadLine(); } else { Console.WriteLine("You dont encounter any enemies!"); Console.ReadLine(); goto A; } while (hhp>0 && fight>0 && hhp>0) { switch (nom) { case 1: B:Console.WriteLine("What would you like to do?"); Console.WriteLine("attack ---- defend"); Console.WriteLine("Health " + hhp + "."); Console.WriteLine("Monster's Health " + mhp + "."); choice = Console.ReadLine(); if (choice == "attack" || choice == "Attack") { Random hdmgr; hdmgr = new Random(); hdmg = hdmgr.Next(1, 25); Random haccr; haccr = new Random(); hacc = haccr.Next(0, 101); if (hacc >= 20) //I should add the monsters damage in here too. { mhp = mhp - hdmg; Console.WriteLine("Your attack hits the enemy. The monster has " + mhp + " health remaining"); Random mdmgr; mdmgr = new Random(); mdmg = mdmgr.Next(1, 25)- hdmg; hhp = hhp - mdmg; switch (hdmg) { case (hdmg>=1 && hdmg <=4): hhp = hhp - mdmg; hhp = hhp - 5; Console.WriteLine("Your weak attack barely hurts the monster who strikes back with ease for " +(mdmg+5)+" damage"); break; case (hdmg>=5 && hdmg <=16): Random maccr; maccr = new Random(); macc = maccr.Next (1, 100); if (macc >= 40) { hhp = hhp - mdmg; Console.WriteLine("The monster powers on attacking you for " +mdmg+ " health."); } else { Console.WriteLine("The monster misses it's attack!"); } break; case (hdmg>=17): Console.WriteLine("Your attack is so powerful the enemy is unable to counter it!"); break; } } else { Console.WriteLine("You miss the monster!"); Random maccr; maccr = new Random(); macc = maccr.Next (1, 100); if (macc >= 15) { hhp = hhp - mdmg; Console.WriteLine("The monster hits you for " +mdmg+" health."); } } } else { Random defr; defr = new Random(); def = defr.Next(1, 101); if (def >= 35 && choice == "defend" || choice == "Defend") { Console.WriteLine("You defended the enemy's attack!"); } else if (choice == "defend" || choice == "Defend") { Random hdmgr; hdmgr = new Random(); hdmg = hdmgr.Next(1, 25); Random mdmgr; mdmgr = new Random(); mdmg = mdmgr.Next(5, 11); hhp = hhp - mdmg; Console.WriteLine ("You were unable to protect yourself from the attack but took less damage!"); } else { Console.WriteLine("That is not a valid choice"); goto B; } } break; case 2: Console.WriteLine("What would you like to do?"); Console.WriteLine("attack ---- defend"); Console.WriteLine("Health " + hhp + "."); Console.ReadLine(); break; case 3: Console.WriteLine("What would you like to do?"); Console.WriteLine("attack ---- defend"); Console.WriteLine("Health " + hhp + "."); Console.ReadLine(); break; default: Console.WriteLine ("CRAP"); break; } } } } }[/code]
How would I go about limiting the number of characters in a std::string to a specific number/pixel length? (C++, Code::Blocks, SFML2)
Thank you so much! That fixed the else statement, now I just need to figure out why it thinks hdmg in the cases is a bool and not a int (Error 1 Cannot implicitly convert type 'bool' to 'int' 121 43 Text Based Battle System)
[QUOTE=MadPro119;30146498][b] Thank you so much! That fixed the else statement, now I just need to figure out why it thinks hdmg in the cases is a bool and not a int (Error 1 Cannot implicitly convert type 'bool' to 'int' 121 43 Text Based Battle System)[/b][/QUOTE] In your switch your telling it to compare it to an int but your case is a bool (hdmg>=1 && hdmg <=4) Your best using an if statement there [editline]30th May 2011[/editline] [QUOTE=MadPro119;30146498][b] Thank you so much! That fixed the else statement, now I just need to figure out why it thinks hdmg in the cases is a bool and not a int (Error 1 Cannot implicitly convert type 'bool' to 'int' 121 43 Text Based Battle System)[/b][/QUOTE] In your switch your telling it to compare it to an int but your case is a bool (hdmg>=1 && hdmg <=4) Your best using an if statement there
[QUOTE=Samuka97;30146492]How would I go about limiting the number of characters in a std::string to a specific number/pixel length?(C++, Code::Blocks, SFML2)[/QUOTE] For monospaced fonts you can do [cpp]static const float singleCharLength = sf::Text("a", font, size).GetRect().Width; const std::size_t length = static_cast<std::size_t>(desiredLength / singleCharLength); if(string.size() > length) string.resize(length);[/cpp] For variable-spaced fonts it's not so fun, since each letter can have a different length. You should probably make an educated guess on the medium size and go from there.
[QUOTE=ZeekyHBomb;30146547]For monospaced fonts you can do [cpp]static const float singleCharLength = sf::Text("a", font, size).GetRect().Width; const std::size_t length = static_cast<std::size_t>(desiredLength / singleCharLength); if(string.size() > length) string.resize(length);[/cpp] For variable-spaced fonts it's not so fun, since each letter can have a different length. You should probably make an educated guess on the medium size and go from there.[/QUOTE] For that to work, what do I need to change exactly? [b]Edit:[/b] Nevermind, I'm stupid. Thanks!
[QUOTE=Richy19;30146517]In your switch your telling it to compare it to an int but your case is a bool (hdmg>=1 && hdmg <=4) Your best using an if statement there [editline]30th May 2011[/editline] In your switch your telling it to compare it to an int but your case is a bool (hdmg>=1 && hdmg <=4) Your best using an if statement there[/QUOTE] Thank you good sir! I studied the switch you made with the else than changed my whole switch to a an if like you said!
[QUOTE=Fox-Face;30146254]Let me post a quick example, hang on Here: You need to implement ISerializeable in your struct, which will contan only the GetObjectData method, this will be used by the BinaryFormatter to serialize your struct, and the constructor will be used by it to unserialize, then you just pass the object to it.[/QUOTE] How would you Serialize a 2D array of floats, or a 2d array of a Vector3 class?
Considering this is a thread partially about Java, I thought I'd toss my problem out in the open. Whenever I try and use the command javac, my command prompt spits back a nasty message saying "'javac' is not recognized as an internal or external command, operable program or batch file". I checked to see if I had my eggs in the right basket with a quick java -version, and it spat back my version information correctly. Here's where I have my Java installed: C:\Program Files\Java\jre6 C:\Program Files\Java\jdk1.6.0_25
Add the first one (I believe) to your System path. It's where CMD looks when you give it pathless things.
Less a programming question, more a compiling issue.. I've installed wxWidgets on my MacBook Pro and it's now in /usr/local/wx-2.9/wx but I'm not sure how I can link to it with G++.. How would I go about doing this? I've tried using the -I switch and adding that directory but it then complains like this: /usr/local/include/wx-2.9/wx/wx.h:15:21: error: wx/defs.h: No such file or directory And then says that a lot more times for other files. How should I correctly be including the wxWidgets library during G++ compiling? Fixed - I didn't type my wx-config thing right first time and assumed it didn't work
could someoe help me im geting dossed alot how can i stop this from happening ty
[QUOTE=adam78;30148992]could someoe help me im geting dossed alot how can i stop this from happening ty[/QUOTE] Try asking in Hardware & Software; this is the Programming forum.
[QUOTE=adam78;30148992]could someoe help me im geting dossed alot how can i stop this from happening ty[/QUOTE] You press ALT + F4
[QUOTE=adam78;30148992]could someoe help me im geting dossed alot how can i stop this from happening ty[/QUOTE] That is not programming related. And it's probably something other than a Denial of Service attack. Have you checked to see if your internet plan sucks?
Is there any way to align SFML text to the right instead of the left? (C::B, SFML2, C++) [editline]31st May 2011[/editline] Nevermind, using some old code from ZeekyHBomb I managed to whip something up real quick: [cpp]float days_left_length = sf::Text(days_left_string, fnt_task_name, 12).GetRect().Width; task_names.SetPosition(iter->x + 230 - (int(days_left_length/2)), iter->y + 4);[/cpp]
[cpp]char plyr1marker; cout << "Player 1, choose your favorite symbol. >"; cin >> plyr1marker; cout << endl; if(plyr1marker == '1','2','3','4','5','6','7','8','9') { std::system("cls"); cout << "Yoo can't have a number.\n"; }[/cpp] I want that if to run only if I type a number 1-9 into the program, however it always runs. How do I stop that from happening?
if ( plyr1marker >= '1' && plyr1marker <= '9' ) [editline]31st May 2011[/editline] I think that works in C++
[QUOTE=Darwin226;30157642]if ( plyr1marker >= '1' && plyr1marker <= '9' ) [editline]31st May 2011[/editline] I think that works in C++[/QUOTE] Thank you, that works, but why was my way bad in the first place? Or is if ( a == x,y,z ) not valid syntax?
Sorry, you need to Log In to post a reply to this thread.