Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 66 67 [68] 69 70 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95702 times)

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1005 on: March 04, 2011, 02:21:43 pm »

I have very little experience with any sort of net-based coding

You want to look at 0MQ, this is actually true for anyone interested in multi-threaded programming too.
CLICK CLICK CLICK CLICK CLICK
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Derekristow

  • Bay Watcher
    • View Profile
    • Steam ID
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1006 on: March 05, 2011, 05:28:37 am »

Mouse collision can actually be quite easy, depending on the amount of support your library is giving. In the case of a 3D game you can draw a ray from the camera through the front of the camera's frustum (a truncated pyramid that determines what should and should not be drawn) till the back of it. Then you can do a ray intersection check to find the object the player is clicking on. (both ray drawing and intersection checking tends to be available in most engines.
That is of course, assuming you're doing 3D. If it's 2D you need size bounds for your object so that you can just plug in the x and y coordinates of the mouse and get the object the mouse is hovering over.

I'm doing it in 2D, but the mouse needs to collide with non-rectangular objects, namely the tiles in the picture.  It's proving more complicated than necessary because I don't always know where the tiles are in the window (I designed it so they would always be centered-ish, and the number of tiles displayed can be changed).
Logged
So my crundles are staying intact unless they're newly spawned... until they are exposed to anything that isn't at room temperature.  This mostly seems to mean blood, specifically, their own.  Then they go poof very quickly.

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1007 on: March 06, 2011, 09:01:45 am »

I seem to have forgotten how pass by ref works in c.
Code: [Select]
$ cat temp.c
#include <stdio.h>

void change(char *i)
{
     char *d = "hello, world";
     i = d;
}

int main ()
{
    char *d = "world, hello";
    change(d);
    puts(d);

    return 0;
}

$ gcc -o temp temp.c
$ ./temp
world, hello
iirc, that should change d to "hello, world".
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1008 on: March 06, 2011, 09:14:30 am »

I seem to have forgotten how pass by ref works in c.

iirc, that should change d to "hello, world".

Code: [Select]
void change(char *&i)
{
     char *d = "hello, world";
     i = d;
}

int main ()
{
    char *d = "world, hello";
    change(d);
    puts(d);

    return 0;
}

There. References are ampersands, and the parameter needs to be *&i to work.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1009 on: March 06, 2011, 09:28:34 am »

C++ uses ampersands in function parameters, I'm using C.
« Last Edit: March 06, 2011, 09:30:47 am by ILikePie »
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1010 on: March 06, 2011, 09:58:35 am »

 :-[

Nevermind, then.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1011 on: March 06, 2011, 12:04:44 pm »

change(&d);
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1012 on: March 06, 2011, 12:35:14 pm »

Tried that too, it doesn't work. iirc d is a pointer pointing to d[0].
Anyway, I dropped the pass by reference, now I'm having trouble getting it to read 16 bytes.
Code: [Select]
$ cat libdib.c
#include  <stdio.h>
#include <stdlib.h>

char *extract_data (FILE *in)
{
     int pos;       // data offset
     long int size; // data size
     size_t len;    // length of data read
     char *d;       // the data

     fseek(in, 0xA, SEEK_SET);
     len = fread(&pos, sizeof(int), 1, in);

     if (len != 1) {
         fputs("read error", stderr);
         return NULL;
     }
     
     fseek(in, 0, SEEK_END);
     size = ftell(in) - pos;
     
     d = (char *)malloc (sizeof(char)*size);
     fseek(in, pos, SEEK_SET);
     len = fread(d, 1, size, in);

     if (len != size) {
         fputs("read error", stderr);
         return NULL;
     }
     return d;
}

int main ()
{
     FILE *f = fopen("./bitmap.dib", "r");
     char *d;
     if (f == NULL) {
         puts("io error");
         return 1;
     }
     d = extract_data(f);
     fclose(f);
     (d != NULL) ? puts(d):puts("");

     return 0;
}
$ gcc -o libdib libdib.c
$ ./libdib && echo "" && ./libdib | hexdump
ÿÿÿÿÿÿ

0000000 ffff ffff ffff 000a                   
0000007
According to hexdump, it reads only 8 bytes.
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1013 on: March 06, 2011, 02:40:46 pm »

haven't read your entire prog, but are you sure that sizeof(int) returns 16 on your system? that's what it looks like you're using for your length, but IIRC 8 bytes is the standard length of an int (i know it varies, but i believe that's the most common)
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1014 on: March 06, 2011, 02:45:21 pm »

int should be 32 or 64 unless you're working on a very old computer or an IC or something. A char is conventionally 8 bits, but an int is usually the length of a word.
Logged

Blank Expression

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1015 on: March 06, 2011, 03:16:41 pm »

int should be 32 or 64 unless you're working on a very old computer or an IC or something. A char is conventionally 8 bits, but an int is usually the length of a word.
This. Under no architecture with which I am familiar is sizeof(int) = 16 (which would be 128-bit). As it's almost certainly an x64 system, a fread of 8 bytes makes sense.

Understand your inputs before you assume your code is wrong. (Better still is to understand your inputs before you start writing code.)
Logged

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1016 on: March 06, 2011, 03:43:29 pm »

try using unsigned ints longs.
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

olemars

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1017 on: March 06, 2011, 04:44:11 pm »

EDIT: nm
« Last Edit: March 06, 2011, 04:46:27 pm by olemars »
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1018 on: March 06, 2011, 04:46:38 pm »

It's a 32 bit machine, an old P4.
The first fread, the one using sizeof(int), is supposed to read an int from the header of the file. The second fread (fread(d, 1, size, in)), is what's causing problems. I know that size = 16, and yet it still reads only 8 bytes.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1019 on: March 06, 2011, 04:56:13 pm »

Dangerous assumptions there. sizeof(int) on your computer does not have to be the same as the computer that wrote the file. When dealing with file headers, always know the byte count of each data element and read that amount explicitly.

Edit: Would help to know the (hex) contents of the file.
« Last Edit: March 06, 2011, 05:06:29 pm by olemars »
Logged
Pages: 1 ... 66 67 [68] 69 70 ... 78