• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=proboardslol;46499568]If I'm parsing a text file, and I want to add each character to a dynamically allocated multidimensional array, I could read the textfile once, get a bunch of other data from it, and at the same time add the characters to the array without having to get the total # of characters, the longest individual line in the text file, and the total number of carriage returns, then having to rewind the file, malloc the array based on the aforementioned data, and then load it into the array by running the same loop [i]again[/i]. I mean I'm probably going about the entire ordeal totally wrong, but this is just my train of that. edit: don't look at the fact that it's a for loop, since I wouldn't need to do something so obviously fucking stupid, I'm just wondering if you can iteratively malloc a multidimensional array using data that you don't know ahead of time[/QUOTE] That's what realloc is for, as seen in my [URL="http://facepunch.com/showthread.php?t=1250528&p=46497548&viewfull=1#post46497548"]post above[/URL]. If you wanted to make it multidimensional, you'd need to change it a little, of course. Is there any reason you are forcing yourself to use C, and not C++ (and it's standard library)? Using std::vector, in that case, would be a lot easier.
[QUOTE=Tommyx50;46499641]That's what realloc is for, as seen in my [URL="http://facepunch.com/showthread.php?t=1250528&p=46497548&viewfull=1#post46497548"]post above[/URL]. If you wanted to make it multidimensional, you'd need to change it a little, of course. Is there any reason you are forcing yourself to use C, and not C++ (and it's standard library)? Using std::vector, in that case, would be a lot easier.[/QUOTE] I am using C++, just the C standard library. I'm just starting so I'm just sort of experimenting. Not working on any major projects or anything. I'm still trying to figure out vectors as well. Also I like C. idk I just like making things intentionally hard and as low level as possible.
[QUOTE=proboardslol;46499676]I am using C++, just the C standard library. I'm just starting so I'm just sort of experimenting. Not working on any major projects or anything. I'm still trying to figure out vectors as well. Also I like C. idk I just like making things intentionally hard and as low level as possible.[/QUOTE] Low level is pretty fun. Anyways, if you are loading a text file completely into memory, it'd make more sense just to allocate all the memory up-front by figuring out the file size, unless you are working with absurdly huge text files!
[QUOTE=Tommyx50;46500048]Low level is pretty fun. Anyways, if you are loading a text file completely into memory, it'd make more sense just to allocate all the memory up-front by figuring out the file size, unless you are working with absurdly huge text files![/QUOTE] Well I want to make it a multidimensional array, for example. That way there's a row in the array for each carriage return. If I get just the total characters, or size, it won't give me the ability to load each character into a corresponding Row and Column (x and y) coordinate. However, what I can do instead I guess is something like... [code] // assuming bufferArray is a single-dimensional //array that has been loaded with every character in //the text file, including carriage returns int totalRows; //total # of carriage returns + EOF for safety int maxWidth; //total characters in longest row int currentRowPos; //current x position in current row for(int i = 0; i < UBOUND(bufferArray); i++){ currentRowPos++; if(bufferArray[i] == '\n'){ totalRows++; if(maxWidth < currentRowPos){ maxWidth = currentRowPos; //set maxWidth to widest known row currenRowPos = 0; } } else if(bufferArray[i] == EOF){ totalRows++; } } [/code] this way I can use totalRows and maxWidth to malloc a 2 dimensional array like: [code] char** mapArray; *mapArray = malloc(sizeof(char) * totalRows); // malloc height for(int i = 0; i < totalRows; i++){ *mapArray[i] = malloc(sizeof(char) * maxWidth); //malloc width } [/code] now that I have the array, I guess I could just do this: [code] int currentRow = 0; //Current Row, i.e: how many carriage returns have been read int currentRowPos = 0; //Current Row Position, x coordinates of current row for(int i = 0; i < UBOUND(bufferArray); i++){ //maxWidth * totalRows should (theoretically) equal UBOUND(bufferArray) mapArray[currentRow][currentRowPos] = bufferArray[i]; //put iterated bufferArray char into corresponding mapArray Location. //if the textfile were a perfect rectangle //(square or otherwise) I could simply //divide i/currentRow, but I can't rely on //that, so I use currentRowPos with its own //iterations currentRowPos++; if(bufferArray[i] == '\n'){ //reset cursor, drop to next line. currentRow++; currentRowPos = 0; } } [/code] edit: I give up on trying to figure out facepunch's syntax highlighting
In an embedded environment (mbed), I'm having trouble with memory To figure out how much memory I have available, I'm using this: [code] #include "mbed.h" Serial pc(USBTX, USBRX); int main() { char* dummy; int index = 0; while(1) { dummy = new char; pc.printf("%u\r\n", index); ++index; } } [/code] That gets to about 3.4k before quitting. The mbed itself is here: [url]http://developer.mbed.org/platforms/mbed-LPC1768/[/url] Quoted to have 32kb of RAM. Interestingly, when I do this, it works: [code] #include "mbed.h" Serial pc(USBTX, USBRX); int main() { char dummy[10000]; while(1) { dummy[index] = 0x45; pc.printf("%u\r\n", index); ++index; } } [/code] It goes right the way up to 10k before quitting, like it should do. So the stack seems to have more memory available than the heap...? What's really going on here? I've spent hours trying to figure this out as part of a large project, and it seems I'm hitting the memory limit. I couldn't believe I'd hit the memory limit until I ran this test and discovered I was seemingly limited to 3.4KB of RAM. Thanks for any help in advance, I'm tearing my hair out over this.
[IMG]http://puu.sh/cSE8F/9af06cb3d8.png[/IMG] I've been trying to download SFML and SFML.Net for the past week or so, and I keep getting this network error. I tried compiling the source code but it just gave me a fucking headache and ugh. Can someone else try and see if they're getting the same error?
-snip-
[QUOTE=proboardslol;46500156]Well I want to make it a multidimensional array, for example. That way there's a row in the array for each carriage return. If I get just the total characters, or size, it won't give me the ability to load each character into a corresponding Row and Column (x and y) coordinate. However, what I can do instead I guess is something like... this way I can use totalRows and maxWidth to malloc a 2 dimensional array like: now that I have the array, I guess I could just do this: edit: I give up on trying to figure out facepunch's syntax highlighting[/QUOTE] Why exactly are you trying to do?
what do you need help with - proboardslol edition i think you need to read some c++ books man, try the stackoverflow book list
I'm trying to make it so when a bullet is shot from a player, it accounts for the players angle (working) [I]and[/I] velocity (not working). [code] float tvx = (float) (speed * Math.sin(Math.toRadians(currentPlayer.getAngle())) + currentPlayer.getVelocity().x); float tvy = -1 * (float) (speed * Math.cos(Math.toRadians(currentPlayer.getAngle())) + currentPlayer.getVelocity().y); v = new Vector2f(tvx, tvy);[/code] This method doesn't seem to be working (doesn't have any noticeable effect). What can I do to make it so that the bullet also travels based on the player's velocity? [editline]16th November 2014[/editline] Speed is just the speed the bullet should travel at.
[QUOTE=NixNax123;46501536]I'm trying to make it so when a bullet is shot from a player, it accounts for the players angle (working) [I]and[/I] velocity (not working). [code] float tvx = (float) (speed * Math.sin(Math.toRadians(currentPlayer.getAngle())) + currentPlayer.getVelocity().x); float tvy = -1 * (float) (speed * Math.cos(Math.toRadians(currentPlayer.getAngle())) + currentPlayer.getVelocity().y); v = new Vector2f(tvx, tvy);[/code] This method doesn't seem to be working (doesn't have any noticeable effect). What can I do to make it so that the bullet also travels based on the player's velocity? [editline]16th November 2014[/editline] Speed is just the speed the bullet should travel at.[/QUOTE] Something like this should work: [code]float angle = Math.toRadians(currentPlayer.getAngle()); Vector2f forward = new Vector2f(Math.cos(angle), Math.sin(angle)); float inheritedVelocity = 0.2f; // Changing this number will change how much of the player's velocity is added to the bullet. Vector2f bulletVelocity = forward * speed + currentPlayer.getVelocity() * inheritedVelocity;[/code]
[QUOTE=NixNax123;46501536]I'm trying to make it so when a bullet is shot from a player, it accounts for the players angle (working) [I]and[/I] velocity (not working). [code] float tvx = (float) (speed * Math.sin(Math.toRadians(currentPlayer.getAngle())) + currentPlayer.getVelocity().x); float tvy = -1 * (float) (speed * Math.cos(Math.toRadians(currentPlayer.getAngle())) + currentPlayer.getVelocity().y); v = new Vector2f(tvx, tvy);[/code] This method doesn't seem to be working (doesn't have any noticeable effect). What can I do to make it so that the bullet also travels based on the player's velocity? [editline]16th November 2014[/editline] Speed is just the speed the bullet should travel at.[/QUOTE] just work with vectors man...
Just started learning C, and using Cygwin with gcc package to compile the .c files. I'm doing a small program that takes input of x and y, the end has to be something like this: "The x to the power of y is: result" [CODE]#include <stdio.h> int main() { int base, exp; long long int result; printf("Please enter the base and the exponent: "); scanf("%d%d", &base, &exp); [B]for (base=1; base<exp; base++)[/B] { result*=base; } printf("The result of %d raised to power %d is %d", base, exp, result); }[/CODE] The bolded lines are probably where I did things very wrong, but I can't seem to fix the damn thing. Also, another one is taking intervals, implying the users will always input the smaller one first, show all the prime numbers between those intervals. The condition is that you can only use basics stuff like for/while loops and +=, %=, etc only use <stdio.h>
[QUOTE=rikimaru6811;46503476]Just started learning C, and using Cygwin with gcc package to compile the .c files. I'm doing a small program that takes input of x and y, the end has to be something like this: "The x to the power of y is: result" [CODE]#include <stdio.h> int main() { int base, exp; long long int result; printf("Please enter the base and the exponent: "); scanf("%d%d", &base, &exp); [B]for (base=1; base<exp; base++)[/B] { result*=base; } printf("The result of %d raised to power %d is %d", base, exp, result); }[/CODE] The bolded lines are probably where I did things very wrong, but I can't seem to fix the damn thing. Also, another one is taking intervals, implying the users will always input the smaller one first, show all the prime numbers between those intervals. The condition is that you can only use basics stuff like for/while loops and +=, %=, etc only use <stdio.h>[/QUOTE] When initializing result, set its value to 1. Also, if I'm reading your code correctly, inputting 2 for base and 5 for exponent will output 24. You're setting the value of base to the first thing you input then immediately setting its value to 1 in the for loop. [sp]Instead of the for loop being 'for(base=1;base<exp;base++)', do something like 'for(int i=0;i<exp;i++) and it should work[/sp]
Oh shit it works (though I had to declare i outside due to not being in C99 mode). Could you explain a bit more on the "for" loop? How the hell result=result*base can get me the correct answer while it has nothing to do with "i"? If I wrote for(i=1; i<=exp; i++) it should work as well, right?
[QUOTE=MilkBiscuit;46500800][IMG]http://puu.sh/cSE8F/9af06cb3d8.png[/IMG] I've been trying to download SFML and SFML.Net for the past week or so, and I keep getting this network error. I tried compiling the source code but it just gave me a fucking headache and ugh. Can someone else try and see if they're getting the same error?[/QUOTE] Use the SFML.Net nuget package
[QUOTE=rikimaru6811;46503637]Oh shit it works (though I had to declare i outside due to not being in C99 mode). Could you explain a bit more on the "for" loop? How the hell result=result*base can get me the correct answer while it has nothing to do with "i"? If I wrote for(i=1; i<=exp; i++) it should work as well, right?[/QUOTE] If you declared i earlier, yes, it should work. As for your first question, you want to multiply by the same number each cycle, not a different number every time. i is just there to designate the amount of cycles.
[QUOTE=BackwardSpy;46502746]Something like this should work: [code]float angle = Math.toRadians(currentPlayer.getAngle()); Vector2f forward = new Vector2f(Math.cos(angle), Math.sin(angle)); float inheritedVelocity = 0.2f; // Changing this number will change how much of the player's velocity is added to the bullet. Vector2f bulletVelocity = forward * speed + currentPlayer.getVelocity() * inheritedVelocity;[/code][/QUOTE] Thanks, man, I'll try that! Also, thanks for not yelling at my at how stupidly obfuscated my code was. I didn't realize it was that bad until I actually put some thought into analyzing it :v: [editline]16th November 2014[/editline] Alright, I'm trying this: [code] Vector2f forward = new Vector2f((float)Math.sin(angle), -(float)Math.cos(angle)); forward = Vector2f.mul(forward, speed); Vector2f inheritedVelocity = Vector2f.mul(GameScreen.getCurrentPlayer().getVelocity(), playerVelocityScalar); v = Vector2f.add(forward, inheritedVelocity);[/code] Still not really affecting anything (it might be making the bullet's velocity go in the opposite direction I want it, but I can't tell). [editline]16th November 2014[/editline] I'm also using (sin, -cos) because that's what works with the player angle set at where the mouse is.
[QUOTE=Tommyx50;46501003]Why exactly are you trying to do?[/QUOTE] Tilemapping [editline]16th November 2014[/editline] [QUOTE=elevate;46501092]what do you need help with - proboardslol edition i think you need to read some c++ books man, try the stackoverflow book list[/QUOTE] I'm sorry :c
[QUOTE=NixNax123;46504080]Thanks, man, I'll try that! Also, thanks for not yelling at my at how stupidly obfuscated my code was. I didn't realize it was that bad until I actually put some thought into analyzing it :v: [editline]16th November 2014[/editline] Alright, I'm trying this: [code] Vector2f forward = new Vector2f((float)Math.sin(angle), -(float)Math.cos(angle)); forward = Vector2f.mul(forward, speed); Vector2f inheritedVelocity = Vector2f.mul(GameScreen.getCurrentPlayer().getVelocity(), playerVelocityScalar); v = Vector2f.add(forward, inheritedVelocity);[/code] Still not really affecting anything (it might be making the bullet's velocity go in the opposite direction I want it, but I can't tell). [editline]16th November 2014[/editline] I'm also using (sin, -cos) because that's what works with the player angle set at where the mouse is.[/QUOTE] Whenever your testing, you want to be 100% sure if your code actually works or not. Try setting your inherited speed high and bullet velocity ridiculously low so you can see whether it's actually being affected or not - don't keep it extremely high and then guess, because the bullet speed would be so high you can barely tell the difference! [editline]16th November 2014[/editline] [QUOTE=proboardslol;46504241]Tilemapping [editline]16th November 2014[/editline] I'm sorry :c[/QUOTE] Just assume the map is a rectangle, and the first few bytes of your file pre-determine the height and width of your file. That's the easiest way - resizing each row independently is something that you'd never need, and would be really weirdly set out in memory. EDIT: The alternative way is to store the map as a huge string in memory, instead of trying to convert it to a 2d array (where having differently sized rows is weirdly and wouldn't really work). In that instance, whenever you wanted to access the data and change something, you'd need to convert your x, y coordinates into a string position instead of accessing and array directly. Something like this: [code] #include <iostream> char mapString[500] = // Completely arbitrary limit of 500 chars "0000000000\n" "0xxxxxxxx0\n" "0x000000x0\n" "0x000000x0\n" "0xxxxxxxx0\n" "0000000000"; char * getPos(int x, int y) { char * mapPos = mapString; // Set mapPos to the start of the map int currentRow = 0; int currentCol = 0; while(*mapPos != '\0') { // Loop until the end of the string if (currentCol == x && currentRow == y) { // We're at the correct place! Return the pointer, so we can modify this char elsewhere. return mapPos; } if(*mapPos == '\n') { currentCol = 0; currentRow += 1; } currentCol += 1; mapPos += 1; // Move onto the next character } return nullptr; // Error, invalid x/y positions. } int main() { char * position = getPos(5, 3); // Careful, (0, 0) is the top left, not (1, 1). *position = '2'; /* Now map should be: "0000000000 0xxxxxxxx0 0x000000x0 0x000200x0 0xxxxxxxx0 0000000000" */ std::cout << mapString; return 0; } [/code] Facepunch screwed up the formatting, so [URL="http://pastebin.com/TK4PbRKk"]here's a pastebin[/URL]... Anyways, this was is a lot slower (and uglier). I wouldn't advise it. EDIT: Wow, even the pastebin's formatting is messed up. goddamnit. Tried to fix the formatting here...
[QUOTE=Tommyx50;46504391]Whenever your testing, you want to be 100% sure if your code actually works or not. Try setting your inherited speed high and bullet velocity ridiculously low so you can see whether it's actually being affected or not - don't keep it extremely high and then guess, because the bullet speed would be so high you can barely tell the difference! [editline]16th November 2014[/editline] [/QUOTE] I got it to work! Thank you!
I need a bit of assistance with a project I'm doing for AP Comp Sci. I need to create a spreadsheet using Java, similar to Microsoft Excel, but I have no real idea of where to begin. The only thing we need to do right now is to have the program print out the spreadsheet and exit, which is shown [url=http://puu.sh/cTq8B.png]here,[/url] and contains a Spreadsheet class, Cell class, and a Main class w/ the methods. All I really need help with is knowing how to actually start this out. Any ideas?
I'm trying to implement a system, like in Terrara, where, when you're at the edges of a map, the camera stops centering around the player. I've already got the camera to center around the player, but how would I accomplish the edge cases?
[QUOTE=Tommyx50;46504391]Whenever your testing, you want to be 100% sure if your code actually works or not. Try setting your inherited speed high and bullet velocity ridiculously low so you can see whether it's actually being affected or not - don't keep it extremely high and then guess, because the bullet speed would be so high you can barely tell the difference! [editline]16th November 2014[/editline] Just assume the map is a rectangle, and the first few bytes of your file pre-determine the height and width of your file. That's the easiest way - resizing each row independently is something that you'd never need, and would be really weirdly set out in memory. EDIT: The alternative way is to store the map as a huge string in memory, instead of trying to convert it to a 2d array (where having differently sized rows is weirdly and wouldn't really work). In that instance, whenever you wanted to access the data and change something, you'd need to convert your x, y coordinates into a string position instead of accessing and array directly. Something like this: [code] char mapString = "0000000000 0xxxxxxxx0 0x000000x0 0x000000x0 0xxxxxxxx0 0000000000"; char * getPos(int x, int y) { char * mapPos = mapString; // Set mapPos to the start of the map int currentRow = 0; int currentCol = 0; while(*mapPos != "/0") { // Loop until the end of the string if (currentCol == x && currentRow == y) { // We're at the correct place! Return the pointer, so we can modify this char elsewhere. return *mapPos; } if(*mapPos == "/n") { currentCol = 0; currentRow += 1; } currentCol += 1; mapPos += 1; // Move onto the next character } return nullptr; // Error, invalid x/y positions. } int main() { char * position = getPos(5, 3); // Careful, (0, 0) is the top left, not (1, 1). *position = "2"; /* Now map should be: "0000000000 0xxxxxxxx0 0x000000x0 0x000200x0 0xxxxxxxx0 0000000000" */ return 0; } [/code][/QUOTE] The reason I don't assume the file is a rectangle is because I have post-script formatting in my method of tilemapping. For example, the map itself may be as such: [code] 000000000 011111110 011111110 000000000 [/code] where each 0 is a wall and each 1 is grass. But in another language (BASIC) I added post-script formatting, where the terminating character for the map is @ and the next section begins the definition of certain elements, for example: [code] 000000000 011A--110 011---110 000000000 [/code] Where A is a sprite, and - is just a blank space. Then, after the map itself, the rest of the file looks like this: [code] @ $SPRITE=box.txt; [/code] the parser then reads $SPRITE= and assigns the sprite to be read as the the data retrieved from box.txt (which is another parser which i also made). This way I'm not limited by the number of ascii characters I can think of for tiles in my map. I can defined tile variables as well. I had also made it so I could do as many sprites as I wanted, as long as I had an assignment for the A in the post-script, such as: [code] 00000000000 0A--A--A--0 0---------0 00000000000 @ $SPRITE=box.txt;car.txt;building.txt; [/code] reading left-to-right, each is thus given the identity of box.txt, car.txt, and building.txt I had different ways of handling this, and it was still only a prototype, but I had it working. [editline]16th November 2014[/editline] [QUOTE=NixNax123;46504688]I'm trying to implement a system, like in Terrara, where, when you're at the edges of a map, the camera stops centering around the player. How would I accomplish this?[/QUOTE] something like... [code] if(player.x > 0 + (cameraWidth/2)){ //Where cameraWidth is the width of your view/camera, and cameraWidth/2 is the center of the camera //center camera on player) } [/code] that would be for the left wall of the area. This way, when the play moves within the edge of the map plus half the camera width, the camera stops following him on the x-axis. for the right wall, it would be: [code] if(player.x < map.Width - (cameraWidth/2)){ //center camera on player }[/code] Repeat with the Y axis. You can combine both X conditionals into one OR statement, and both Y conditionals into one OR statement, but you should keep X and Y separate so that if you're at the left wall, you can still track Y movement.
[QUOTE=proboardslol;46504738]Long stuff[/QUOTE] If you REALLY need it without compromise, I've given the code to handle non-rectangular maps above. (note; the code you quoted is really broken and I wrote it very quickly, I've replaced it with working code above in my original post).
[QUOTE=Tommyx50;46504842]If you REALLY need it without compromise, I've given the code to handle non-rectangular maps above. (note; the code you quoted is really broken and I wrote it very quickly, I've replaced it with working code above in my original post).[/QUOTE] I could simply use a single-dimensional buffer, and then analyze that buffer for carriage returns and widest row, and then malloc an array from that. Or I could try to ditch multiple dimensions altogether and work with one-dimensional arrays and count carriage returns real-time. but I think for my own sake, the first option I gave is easier. edit: oh wait you did pretty much the second one. Hey way to go dude! that really helps! edit: are string automatically ended with \0 in C++? i remember running into that problem with C a lot. Should I manually concatenate \0 to the end of the buffer for safety?
you dont need to null-terminate your strings in c++
String literals are always null-terminated in C and C++. Meaning that [cpp]"asd"[/cpp] contains the bytes [cpp]asd\0[/cpp] std::string doesn't care about null termination. It works fine with null terminated strings and it works with byte arrays that contain null characters in other parts of the buffer. If you initialize a std::string from a C string or a string literal (that is, from a const char*), the std::string will not contain the null terminator. [cpp]std::string str("asd"); // str.length() == 3 - the null terminator is not included in the std::string[/cpp] However, calling std::string's c_str() will always null-terminate the buffer - even if the null terminator is not part of the string.
I've been looking into reading an XML file with Java and I'm not sure how to store the data read. Any suggestions?
[QUOTE=proboardslol;46504738] something like... [code] if(player.x > 0 + (cameraWidth/2)){ //Where cameraWidth is the width of your view/camera, and cameraWidth/2 is the center of the camera //center camera on player) } [/code] that would be for the left wall of the area. This way, when the play moves within the edge of the map plus half the camera width, the camera stops following him on the x-axis. for the right wall, it would be: [code] if(player.x < map.Width - (cameraWidth/2)){ //center camera on player }[/code] Repeat with the Y axis. You can combine both X conditionals into one OR statement, and both Y conditionals into one OR statement, but you should keep X and Y separate so that if you're at the left wall, you can still track Y movement.[/QUOTE] This works (you mean AND instead of OR), except it acts a bit wonky. It only stops the camera from moving if the player is near both the x and y boundaries. [editline]16th November 2014[/editline] [code] public static void handleEdges() { Vector2f playerPos = GameScreen.getCurrentPlayer().getPos(); if (playerPos.x > -GameScreen.getBounds().x + cam.getSize().x / 2 && playerPos.x < GameScreen.getBounds().x - cam.getSize().x / 2) { camPos = GameScreen.getCurrentPlayer().getPos(); } if (playerPos.y > -GameScreen.getBounds().y + cam.getSize().y / 2 && playerPos.y < GameScreen.getBounds().y - cam.getSize().y / 2) { camPos = GameScreen.getCurrentPlayer().getPos(); } cam.setCenter(camPos);[/code] [editline]16th November 2014[/editline] what's happening: [vid]http://a.pomf.se/guwkof.webm[/vid]
Sorry, you need to Log In to post a reply to this thread.