Do not use SDL_Net's to/from network byte order conversion functions
authorunc0rr
Tue, 20 Feb 2007 16:58:53 +0000
changeset 459 95163c6efa69
parent 458 5880af7b530a
child 460 3242b42ecad4
Do not use SDL_Net's to/from network byte order conversion functions
hedgewars/SDLh.pas
--- a/hedgewars/SDLh.pas	Mon Feb 19 20:00:55 2007 +0000
+++ b/hedgewars/SDLh.pas	Tue Feb 20 16:58:53 2007 +0000
@@ -383,10 +383,10 @@
 function SDLNet_AddSocket(_set: PSDLNet_SocketSet; sock: PTCPSocket): LongInt; cdecl; external SDL_NetLibName;
 function SDLNet_CheckSockets(_set: PSDLNet_SocketSet; timeout: LongInt): LongInt; cdecl; external SDL_NetLibName;
 
-procedure SDLNet_Write16(value: Word; buf: pointer); cdecl; external SDL_NetLibName;
-procedure SDLNet_Write32(value: LongWord; buf: pointer); cdecl; external SDL_NetLibName;
-function SDLNet_Read16(buf: pointer): Word; cdecl; external SDL_NetLibName;
-function SDLNet_Read32(buf: pointer): LongWord; cdecl; external SDL_NetLibName;
+procedure SDLNet_Write16(value: Word; buf: pointer);
+procedure SDLNet_Write32(value: LongWord; buf: pointer);
+function SDLNet_Read16(buf: pointer): Word;
+function SDLNet_Read32(buf: pointer): LongWord;
 
 implementation
 
@@ -396,4 +396,56 @@
        or(( surface^.flags and (SDL_HWSURFACE or SDL_ASYNCBLIT or SDL_RLEACCEL)) <> 0)
 end;
 
+procedure SDLNet_Write16(value: Word; buf: pointer);
+begin
+{$IFDEF LITTLE_ENDIAN}
+  PByteArray(buf)^[0]:= value;
+  PByteArray(buf)^[1]:= value shr 8
+{$ELSE}
+  PByteArray(buf)^[1]:= value;
+  PByteArray(buf)^[0]:= value shr 8
+{$ENDIF}
+end;
+
+procedure SDLNet_Write32(value: LongWord; buf: pointer);
+begin
+{$IFDEF LITTLE_ENDIAN}
+  PByteArray(buf)^[0]:= value;
+  PByteArray(buf)^[1]:= value shr  8;
+  PByteArray(buf)^[2]:= value shr 16;
+  PByteArray(buf)^[3]:= value shr 24
+{$ELSE}
+  PByteArray(buf)^[3]:= value;
+  PByteArray(buf)^[2]:= value shr  8;
+  PByteArray(buf)^[1]:= value shr 16;
+  PByteArray(buf)^[0]:= value shr 24
+{$ENDIF}
+end;
+
+function SDLNet_Read16(buf: pointer): Word;
+begin
+{$IFDEF LITTLE_ENDIAN}
+  SDLNet_Read16:= PByteArray(buf)^[0] or
+                 (PByteArray(buf)^[1] shl 8)
+{$ELSE}
+  SDLNet_Read16:= PByteArray(buf)^[1] or
+                 (PByteArray(buf)^[0] shl 8)
+{$ENDIF}
+end;
+
+function SDLNet_Read32(buf: pointer): LongWord;
+begin
+{$IFDEF LITTLE_ENDIAN}
+  SDLNet_Read32:=  PByteArray(buf)^[0] or
+                  (PByteArray(buf)^[1] shl  8) or
+                  (PByteArray(buf)^[2] shl 16) or
+                  (PByteArray(buf)^[3] shl 24)
+{$ELSE}
+  SDLNet_Read32:=  PByteArray(buf)^[3] or
+                  (PByteArray(buf)^[2] shl  8) or
+                  (PByteArray(buf)^[1] shl 16) or
+                  (PByteArray(buf)^[0] shl 24)
+{$ENDIF}
+end;
+
 end.