Flopid

Members
  • Content count

    81
  • Joined

  • Last visited

Community Reputation

122 Neutral

About Flopid

  • Rank
    Member
  1. Java Deadlock Question

    Well I understand completely what is happening now. I did not really think that Java would obliterate synchronization. Thanks for the clarification frob!!!     I am kind of thinking ahead when in the future I would have a scene with many objects appearing and dissapearing or changing shapes. For things like buffers in that instance multithreading would be a great boost (think OpenCL oh noes). I also think that starting to thread simple code is much easier than threading at the end where u can miss something and might have to debug... for a while : )   Plus I am just doing this for fun to learn more about Java and graphics.   Again many thanks for your replies and have a great day!
  2. Hello. I've got back into programing with OpenGL (JOGL) since it's so portable. I wanted to make my engine multithreaded so I've read up a Java trail about concurency and stumbled on something interesting here:   http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html   How can both threads enter bow if it is synchronized, so that as the article says, they will both be stuck at bowback? Is it possible for threads to enter synchronized methods at the exact same time, but not right after eachother then or wha?   This would completely mess up the whole sync identifier, since threads can pretty much always call the method at the same time...   Really weird, watcha think?
  3. I usually do try to follow OOP without globals, but it seems when, for example, you have a variable that you have passed through a function or a constructor to many classes you pretty much have to somehow document it anyway, for code clarity. In this case it would seem like a global would be just a bit easier to manage. That, however, raises a question, why did you bite the bullet, and thus leaves the scope of this book.
  4. Wow a lot of info from all of the posts, just for a global variable. I am not completely sure how to implement a singleton yet, but I found this website. There are a few questions I have about this, but it is probably a subject for another thread. It seems as if a singleton is much better to use in a team environment, where people can accidentaly instantiate a global more than once, plus as I see it is more safe for threads. I'll have to reasearch more, but for now I'll probably just use "extern" as I can keep track of the code I have.   This bump is not intentional, just wanted to contribute to the conversation : )
  5. Ah lol the extern, completely forgot it existed. I'll take your advice, but I like to make a mess also though, makes me look like I've been working : )   Thank you very much!!!
  6. I've just recently learned from my program that "static" or pointer variables do not neccesseraly mean global program wide. I have a single .h file included for main .cpp and another .cpp file. When I changed the variable in main and read it in the other class it was not the same value as I've set it in the main class, even though it was declared static, or as I've tested even if it is a pointer, in the .h file.   Seems like the only way in C++ to have access to a variable you would like to use in another class is to pass it through a function or a constructor as well as using inheritance. As I recall in Java or C# a static variable or a function can be defined in one class and changed from a class while changing it for all the other classes.   It may be breaking the laws of OOP but I was just wondering if it was possible in C++.   Thank you, and have a nice day!!!
  7. Hello I am having a bit of confusion with C++ and passing a 1D array for vertices into a function when it will be used to fill in the vertex buffer. If I just do this: [code] { GLfloat aVertices[] = { -0.8f, -0.8f, 0.0f, 1.0f, 0.0f, 0.8f, 0.0f, 1.0f, 0.8f, -0.8f, 0.0f, 1.0f }; ... CreateVBO(p_vertices, aVertices); } void CreateVBO(Vertex* theVerts, GLfloat theGLfloat[]) { ... glBufferData(GL_ARRAY_BUFFER, sizeof(theGLfloat), theGLfloat, GL_STATIC_DRAW); ... } [/code] The triangle will not render!!! but if I insert this code inside the CreateVBO function it works: [code] void CreateVBO(Vertex* theVerts, GLfloat theGLfloat[]) { GLfloat newTestVerArr[12]; for(int i = 0; i < 12; i++) { newTestVerArr[i] = theGLfloat[i]; } ... glBufferData(GL_ARRAY_BUFFER, sizeof(newTestVerArr), newTestVerArr, GL_STATIC_DRAW); ... } [/code] It seems like a waste of processing power to copy an array again, so I was wondering if there is a syntax I could use to skip this copy loop. I am confused, since as far as I know the "newTestVerArr" is just a pointer to the first element in the array, just like the "theGLfloat" so why it won't work without the copy I am not sure. If you could explain this to me I would appreciate it. Thanks
  8. Personally I find using escape sequences a lot easier than Environment. It's just less typing. Maybe it is more useful if you are writing a word processor or something of that sort. Then again you could do something like this if you want to add a tab, for example, when you hit a tab key. string c = "word processor text"; if (key == tab) { c += "\t"; } Some people say to code one way is clearer, but I think it is just a style or personal preference. It really depends if you are working with a team or not. That way everyone can settle on one style.
  9. Why use char in C#... when you have string. I guess you could if you make an array for something special.
  10. Yeah lol I thought it would be possible under Linux. Thank you for all of your replies guys.
  11. I am not trying to achieve anything really, just wanted to see if it's possible to make a timer like that. I guess the only way to do it is without threads since they wake up anytime they want... About running 4 threads runnning at a time, they are very small and when I ran it it only used like 2% of the 4 cores I have. When I used the querryfrequency etc it used 25% of all cores for the whole period of the program.
  12. Heh, I wanted to see if I could make a timer that doesn't use all of the processing power(well at least not one whole core). I was not very successful but technically it should work. I'll explain before posting code. Say you want to have a timer that clicks every 250 microseconds, what you do is create 4 threads at 250 mics intervals and make them sleep for 1 millisecond. You will need more threads if you are doing faster timings or less if you are doing slower timings. That was my idea but the problem is that threads seem to do what ever they want especially when there is other loads on the CPU. So here is the code. I don't know how to make it work better and maybe you have an idea. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Timers; using System.Runtime.InteropServices; namespace Imba_Timer { class Program { public Thread[] tCountHF; public Thread tCountLF; public static int timeHF = 0; public static int timeLF = 0; [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceCounter( out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency( out long lpFrequency); Program() { tCountLF = new Thread(ShowTime); } Program(int numThreads) { tCountHF = new Thread[numThreads]; for (int i = 0; i < numThreads; i++) { tCountHF[i] = new Thread(SleepOne); } } static void Main(string[] args) { Program myPrg = new Program(); myPrg.ThreadsStartup(250); Console.ReadLine(); } public void StartUpCounter(int microSeconds) { float stepTimeSec = (float)1 * (float)(microSeconds / 1000000) ; int x = 100; float secondsPerOpp = 0; float secondsPerOpp2 = 0; float incrTime = 0; long cntsPerSec = 0; long prevTimeStamp = 0; long currTimeStamp = 0; long prevTimeStamp2 = 0; long currTimeStamp2 = 0; QueryPerformanceFrequency(out cntsPerSec); do { QueryPerformanceCounter(out prevTimeStamp2); QueryPerformanceCounter(out prevTimeStamp); while (x > 0) { x--; } x = 100; QueryPerformanceCounter(out currTimeStamp); secondsPerOpp = (currTimeStamp - prevTimeStamp) * (1.0f / (float)cntsPerSec); QueryPerformanceCounter(out currTimeStamp2); secondsPerOpp2 = (currTimeStamp2 - prevTimeStamp2) * (1.0f / (float)cntsPerSec); incrTime += (secondsPerOpp + secondsPerOpp2); } while (incrTime < stepTimeSec); } public void ThreadsStartup(int microSec) { int numThreads = 1000 / microSec; Program start = new Program(numThreads); Program lowFrequency = new Program(); lowFrequency.tCountLF.Start(); for (int i = 0; i < numThreads; i++) { start.tCountHF[i].Priority = ThreadPriority.Highest; start.tCountHF[i].Start(this); start.StartUpCounter(microSec); } } public void SleepOne(object obj) { //while (!stop) for (int g = 0; g < 120000; g++) { Thread.Sleep(1); timeHF++; } } public void ShowTime() { //while (!stop); for (int g = 0; g < 120; g++) { Thread.Sleep(1000); timeLF += 1000; Console.WriteLine("Hight Frequency Time is: " + timeHF + " ------ " + "Low Frequency Time is: " + timeLF); } } } } For now this code is not very useful since most of these high frequency timings are used in something other than games where you can use the Sleep() function. Plus it is not very precise which I do not know why. Oh well was worth a shot.
  13. Errrr, I am wrong... I put the index of the last element as a lenth parameter which the substring takes. Rycross was right, I dind't understand the substring completely. Thank you for correcting me.
  14. Sorry I did not write a more error proof code, just didn't know I need to, since it works if you know what you are doing. Thought a simple code would just help people who struggle with this stuff. Also I used the substring(start, to) for just extracting the part I need from the html code, and it always gave me an error saying that index is out of range. It seems to ignore all the escape chars like \n \t \ etc.. So it basicly searches in the string that is smaller (ie without escape chars). However the Substring(start) does not ignore them. So that is why I delete after a certain point. You could try that with some website's html code and you'll see.
  15. I've been trying to use the substring(start, end) function on HTML code, and it doesn't work right because it seems as if this particular function doesn't count the escape characters. So I found a way to go around it. Here is the code I use to get a certain substring from HTML. Maybe this will also ignite Microsoft to put this code in their other form of substring. public string ImprovedSubstring(string fullString, string startString, string endString) { string theSubstring = ""; int i1 = fullString.IndexOf(startString); fullString = fullString.Substring(i1); int i2 = fullString.IndexOf(endString); theSubstring = fullString.Remove(i2 + endString.Length); return theSubstring; } LoL there is a plus sign in the last Remove method, some bug on gamedev doesn't show it in preview. Thank you and hope this helps a little with escape chars.