0 Members and 1 Guest are viewing this topic.
#include <os.h>#include <SDL/SDL.h>#include <fdlibm.h>int main(void) { SDL_Surface *screen; SDL_Surface **ship; SDL_Surface *shots; SDL_Rect shipPos; SDL_Rect *shotPos; static unsigned short **shipSprites; static unsigned short *shotSprites; int shipAnim = 0; int *activeShot; int currentShot = 0; int shootDelay = 0; int i, j; // Malloc for SDL_Surface *screen screen = malloc(sizeof(SDL_Surface)); if(screen == NULL) exit(0); // Malloc for SDL_Surface *ship[3] ship = malloc(3*sizeof(SDL_Surface)); if(ship == NULL) { free(screen); exit(0); } for(i = 0; i < 3; i++) { ship[i] = malloc(sizeof(SDL_Surface)); if(ship[i] == NULL) { free(screen); for(j = 0; j < i; j++) free(ship[j]); free(ship); exit(0); } } // Malloc for SDL_Surface *shots shots = malloc(sizeof(SDL_Surface)); if(shots == NULL) { free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); exit(0); } // Malloc for SDL_Rect shotPos[128] shotPos = malloc(128 * sizeof(SDL_Rect)); if(shotPos == NULL) { free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); free(shots); exit(0); } // Malloc for int activeShot[128] activeShot = malloc(128 * sizeof(int)); if(activeShot == NULL) { free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); free(shots); free(shotPos); exit(0); } for(i = 0; i < 128; i++) activeShot[i] = 0; // Malloc for static unsigned short shipSprites[3][1408] shipSprites = malloc(3 * sizeof(*shipSprites)); if(shipSprites == NULL) { free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); free(shots); free(activeShot); free(shotPos); exit(0); } for(i = 0; i < 3; i++) { shipSprites[i] = malloc(1408 * sizeof(unsigned short)); if(shipSprites[i] == NULL) { free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); free(shots); for(j = 0; j < i; j++) free(shipSprites[j]); free(shipSprites); free(activeShot); free(shotPos); exit(0); } } #include "shipSpritesC.util" // Malloc for static unsigned short shotSprites[49] shotSprites = malloc(49 * sizeof(unsigned short)); if(shotSprites == NULL) { free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); free(shots); for(i = 0; i < 3; i++) free(shipSprites[i]); free(shipSprites); free(activeShot); free(shotPos); exit(0); } #include "shotSpritesC.util" SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(320, 240, is_cx?16:8, SDL_SWSURFACE); shots = nSDL_LoadImage(shotSprites); for(i = 0; i < 3; i++) ship[i] = nSDL_LoadImage(shipSprites[i]); for(i = 0; i < 127; i++) activeShot[i] = 0; while(!isKeyPressed(KEY_NSPIRE_ESC)) { if(isKeyPressed(KEY_NSPIRE_DOWN) && shipPos.y < 204) shipPos.y++; if(isKeyPressed(KEY_NSPIRE_LEFT) && shipPos.x > 0) shipPos.x--; if(isKeyPressed(KEY_NSPIRE_RIGHT) && shipPos.x < 281) shipPos.x++; if(isKeyPressed(KEY_NSPIRE_UP) && shipPos.y > 0) shipPos.y--; if(isKeyPressed(KEY_NSPIRE_LEFT) && !isKeyPressed(KEY_NSPIRE_RIGHT)) shipAnim = 1; else if (isKeyPressed(KEY_NSPIRE_RIGHT) && !isKeyPressed(KEY_NSPIRE_LEFT)) shipAnim = 2; else shipAnim = 0; if(isKeyPressed(KEY_NSPIRE_CTRL) && !shootDelay) { activeShot[currentShot] = 1; shotPos[currentShot].x = shipPos.x + (ship[shipAnim]->w - shots->w)/2; shotPos[currentShot].y = shipPos.y; currentShot = (currentShot + 1) % 128; shootDelay = 32; } for(i = 0 ; i < 127 ; i++) { if(activeShot[i]) { shotPos[i].y -= 3; SDL_BlitSurface(shots, NULL, screen, &shotPos[i]); if(shotPos[i].y < 4) activeShot[i] = 0; } } SDL_BlitSurface(ship[shipAnim], NULL, screen, &shipPos); SDL_Flip(screen); SDL_FillRect(screen, NULL, (Uint32)0xFFFFFFFF); shootDelay = (shootDelay > 0 ? shootDelay - 1 : 0); } SDL_FreeSurface(shots); SDL_FreeSurface(screen); for(i = 0; i < 3; i++) { SDL_FreeSurface(ship[i]); } free(screen); for(j = 0; j < 3; j++) free(ship[j]); free(ship); free(shots); free(shotSprites); for(i = 0; i < 3; i++) free(shipSprites[i]); free(shipSprites); free(activeShot); free(shotPos); SDL_Quit(); return 0;}
It crashes if I try to display the available memory in any way, even by the Status menu.
#include <os.h>#include <common.h>#include <fdlibm.h>#include "utils.h"#include "texture.h"#define TEXTURE_WIDTH 128#define TEXTURE_HEIGHT 128int main(void){ char *screen; double (*angle_lut)[240]; double (*depth_lut)[240]; double relativeX, relativeY, tempRelative, angle, textureX, textureY; int w = 240, h = 240, x, y, color, screenX, screenY; angle_lut = malloc(240 * sizeof(*angle_lut)); if(!angle_lut) exit(0); depth_lut = malloc(240 * sizeof(*depth_lut)); if(!depth_lut) { free(angle_lut); exit(0); } screen = malloc(SCREEN_BYTES_SIZE * sizeof(char)); if(!screen) { free(angle_lut); free(depth_lut); exit(0); } for(y = 0; y < h; y++) { for(x = 0; x < w; x++) { relativeX = x - w / 2; relativeY = h / 2 - y; tempRelative = relativeX * relativeX + relativeY * relativeY; if(!tempRelative) depth_lut[x][y] = 0.0; else depth_lut[x][y] = 8388608 / tempRelative; if(!relativeY) { if(relativeX < 0) angle = -64; else angle = 64; } else { angle = atan(relativeX / relativeY) / (2*M_PI) * 128; if(relativeY > 0) angle += 128; } angle_lut[x][y] = angle * 8; } } while(!isKeyPressed(KEY_NSPIRE_ESC)) { clearBuf(screen); for(screenY = 0; screenY < h; screenY++) { for(screenX = 0; screenX < w; screenX++) { x = screenX - w/2; y = h/2 - screenY; // X is the angle, Y is the depth textureX = angle_lut[x][y]; textureY = depth_lut[x][y]; while(x < 0) { x += TEXTURE_WIDTH; } while(y < 0) { y += TEXTURE_HEIGHT; } while(x > TEXTURE_WIDTH) { x -= TEXTURE_WIDTH; } while(y > TEXTURE_HEIGHT) { y -= TEXTURE_HEIGHT; } color = texture[(int)textureY % 128][(int)textureX]; ← crashes when reach this line color %= 256; setPixelBuf(screen, screenX+40, screenY, (int)(abs(color-1)/16)); } } refresh(screen); } free(screen); free(angle_lut); free(depth_lut); return 0;}
unsigned short texture[128][128] = {{ 0x2a01, 0x80, 0x80, 0x0, 0xad54, 0x944f, 0xa4b0, 0xa4b0, 0x9c90, 0xa4b0, 0xa4b0, 0xa4b0, 0xa4b0, 0xa46f, 0x9c6f, 0x942f, 0x9c6f, 0x9c4f, 0x9c6f, 0xa490, 0xacd0, 0xacd0, 0xa4b0, 0xacb0, 0x9c4f, 0xa46f, 0x942e, 0x8bed, 0x8c0e, 0x942f, 0x8bee, 0xa470, 0xa470, 0x9c90, 0x9c4f, 0x942e, 0xa470, 0xa490, 0xa4d0, 0xa4d1, 0xa4d1, 0xa4f1, 0xa4d1, 0xa4d1, 0xa4d1, 0xad11, 0xad32, 0xad12, 0xa4b0, 0xa4d0, 0xa4d0, 0xa4d1, 0xa4b0, 0xa4b0, 0x9cb0, 0xa4f1, 0xacf1, 0xa4f1, 0xa4f1, 0xad11, 0xacf1, 0xa4b1, 0xa4d1, 0xb532, 0xb572, 0xa4d1, 0xa4b1, 0xa4b0, 0x9cb0, 0x9c90, 0xa4b0, 0xa4b0, 0x9c6f, 0x9c90, 0x9c90, 0xa4b0, 0xa4f1, 0xa4d0, 0xa4b0, 0x9cb0, 0xa4d0, 0xacf1, 0xa4f1, 0xa4f0, 0xa4d0, 0xad12, 0xad12, 0xa4d0, 0xa4d1, 0xa4d1, 0xa4b0, 0xad32, 0xb553, 0xad11, 0xa4b0, 0xacf1, 0xb532, 0xb552, 0xb552, 0xbd73, 0xb532, 0xa4d1, 0x9c8f, 0xa4d0, 0xa4b1, 0x9c6f, 0x9c8f, 0x946f, 0x946f, 0x9caf, 0x944f, 0x9470, 0x944f, 0x9c70, 0x946f, 0x948f, 0xa4b0, 0x9c8f, 0x9c6f, 0x944f, 0x946f, 0xacf1, 0xa4d0, 0xa4b0, 0x9c6f, 0x9cb0, 0xa4f1, 0x9c6f },{ 0x944f, 0x944f, 0x944f, 0x83ee, 0x7b8c, 0x5a67, 0x6267, 0x5a67, 0x6aa8, 0x6288, 0x6287, 0x6287, 0x6aa7, 0x6267, 0x5a46, 0x5a26, 0x5a46, 0x5a46, 0x6287, 0x6267, 0x5a46, 0x6287, 0x6267, 0x6267, 0x5206, 0x5a46, 0x5206, 0x41a4, 0x4a05, 0x5226, 0x49c5, 0x5a26, 0x5a46, 0x5a46, 0x5a26, 0x5a46, 0x6287, 0x5a67, 0x6287, 0x6287, 0x6287, 0x62a8, 0x6ac8, 0x62a8, 0x6288, 0x6288, 0x6ac8, 0x62a8, 0x62a7, 0x62a8, 0x5a87, 0x6287, 0x62a7, 0x62a7, 0x62a8, 0x6ae8, 0x62a7, 0x6287, 0x6287, 0x6287, 0x6287, 0x6288, 0x6ac8, 0x62c7, 0x5aa7, 0x6287, 0x6288, 0x5a67, 0x5a67, 0x5a67, 0x62a7, 0x5a67, 0x5a67, 0x6287, 0x5a67, 0x5a67, 0x62a8, 0x6287, 0x5a87, 0x5a47, 0x5a67, 0x62a7, 0x62c8, 0x6ae8, 0x62a8, 0x6ae8, 0x6ac8, 0x6287, 0x5a67, 0x62a7, 0x62a8, 0x6ac8, 0x6ac8, 0x6ae8, 0x62c7, 0x62a7, 0x62c8, 0x6ae9, 0x6b09, 0x6ae8, 0x5aa7, 0x5266, 0x5246, 0x5266, 0x5aa7, 0x5266, 0x5226, 0x5226, 0x5a46, 0x5246, 0x4a06, 0x4a06, 0x49e5, 0x5226, 0x4a26, 0x5226, 0x5246, 0x5246, 0x5a66, 0x5246, 0x5226, 0x5a67, 0x5a47, 0x5246, 0x49e5, 0x5226, 0x6287, 0x5226 },etcetera ...};