#176595 - Squeakychu - Tue Aug 23, 2011 7:22 am
Hello. I'm new to DS programming, though I have had programming experience before. I hope to get along with everyone and make some cool games and programs for the DS. Anyways, I have a few problems and questions on some programs I'm trying to make with devkitpro, and I hope this is the right place to get help.
In my first program, I'm trying to display moving stars using random colors in random positions and random speed. I'm also trying to get the DS to read inputs and change the message if a button is inputted. The stars are rendered on the top screen while the button information is on the bottom. Here's the full code for the program.
The program compiles and runs just fine, but the X, Y, and touch buttons don't seem to register, even though it works just fine on it's own. I'm curious what the issue with that is. Also, I don't want stars to move at an integer speed per frame, but ARM9 doesn't handle floating point integers well from what I hear. So I used this code in order to have greater control over my speed without using floating-point variables.
I'm curious if this is the correct way of moving a star less than 1 pixel at a time, or if there's a better or less CPU-intensive method of doing what's intended in this code.
Lastly, I'm trying to get to grips with control of the Main and Sub Displays, so I'm trying to make a program that displays the moving stars on the top screen, while having a simple drawing program run on the bottom screen. I'm trying to set the drawing program to the main display, while setting the moving stars to the sub display, and as you can maybe see in this code, I'm having great difficulty trying to achieve that.
With this program as it is, the drawing part works just fine, but the top screen displays nothing but white. I have no idea how to correct this issue. If anyone has suggestions, that would be just fantastic!
Anyway, these are my major concerns. Thanks for bearing with me, and I hope to have a good experience on these forums. :)
In my first program, I'm trying to display moving stars using random colors in random positions and random speed. I'm also trying to get the DS to read inputs and change the message if a button is inputted. The stars are rendered on the top screen while the button information is on the bottom. Here's the full code for the program.
Code: |
#include <nds.h>
#include <stdio.h> #define NUM_STARS 60 typedef struct { int x; int y; int speed; int speed2; int xpixle; unsigned short color; }Star; Star star[NUM_STARS]; void ClearScreen(void) { int i; for(i = 0; i < 256 * 192; i++) VRAM_A[i] = RGB15(0,0,0); } void InitStars(void) { int i; for(i = 0; i < NUM_STARS; i++) { star[i].color = RGB15(rand() % 15 + 16, rand() % 20 + 11, rand() % 25 + 16); star[i].speed = rand() % 90 + 20; star[i].speed2 = 0; star[i].x = rand() % 256; star[i].y = rand() % 192; star[i].xpixle = 0; } } void EraseStar(Star* star) { VRAM_A[(int)star->x + star->y * SCREEN_WIDTH] = RGB15(0,0,0); } void MoveStar(Star* star) { star->speed2 += star->speed; if(star->speed2 >= 100) { while(star->speed2 >= 100) { star->xpixle++; star->speed2 -= 100; } star->x += star->xpixle; star->xpixle = 0; } if(star->x >= SCREEN_WIDTH) { star->color = RGB15(rand() % 15 + 16, rand() % 20 + 11, rand() % 25 + 16); star->x = 0; star->y = rand() % 192; star->speed = rand() % 90 + 20; } } void DrawStar(Star* star) { VRAM_A[(int)star->x + star->y * SCREEN_WIDTH] = star->color; } int main(void) { int i; irqInit(); irqEnable(IRQ_VBLANK); videoSetMode(MODE_FB0); vramSetBankA(VRAM_A_LCD); ClearScreen(); InitStars(); consoleDemoInit(); while(1) { scanKeys(); if (keysHeld() & KEY_A) printf("Button A is pressed\n"); else printf("Button A is released\n"); if (keysHeld() & KEY_X) printf("Button X is pressed\n"); else printf("Button X is released\n"); if (keysHeld() & KEY_B) printf("Button B is pressed\n"); else printf("Button B is released\n"); if (keysHeld() & KEY_Y) printf("Button Y is pressed\n"); else printf("Button Y is released\n"); if (keysHeld() & KEY_L) printf("Button L is pressed\n"); else printf("Button L is released\n"); if (keysHeld() & KEY_R) printf("Button R is pressed\n"); else printf("Button R is released\n"); if (keysHeld() & KEY_START) printf("Start button is pressed\n"); else printf("Start button is released\n"); if (keysHeld() & KEY_SELECT) printf("Select button is pressed\n"); else printf("Select button is released\n"); if (keysHeld() & KEY_TOUCH) printf("The Touch screen is touched\n"); else printf("The touch screen is not touched\n"); swiWaitForVBlank(); for(i = 0; i < NUM_STARS; i++) { EraseStar(&star[i]); MoveStar(&star[i]); DrawStar(&star[i]); } consoleClear(); } return 0; } |
The program compiles and runs just fine, but the X, Y, and touch buttons don't seem to register, even though it works just fine on it's own. I'm curious what the issue with that is. Also, I don't want stars to move at an integer speed per frame, but ARM9 doesn't handle floating point integers well from what I hear. So I used this code in order to have greater control over my speed without using floating-point variables.
Code: |
void MoveStar(Star* star)
{ star->speed2 += star->speed; if(star->speed2 >= 100) { while(star->speed2 >= 100) { star->xpixle++; star->speed2 -= 100; } star->x += star->xpixle; star->xpixle = 0; } if(star->x >= SCREEN_WIDTH) { star->color = RGB15(31,31,31); star->x = 0; star->y = rand() % 192; star->speed = rand() % 90 + 20; } } |
I'm curious if this is the correct way of moving a star less than 1 pixel at a time, or if there's a better or less CPU-intensive method of doing what's intended in this code.
Lastly, I'm trying to get to grips with control of the Main and Sub Displays, so I'm trying to make a program that displays the moving stars on the top screen, while having a simple drawing program run on the bottom screen. I'm trying to set the drawing program to the main display, while setting the moving stars to the sub display, and as you can maybe see in this code, I'm having great difficulty trying to achieve that.
Code: |
#include <nds.h>
#include <stdio.h> #define NUM_STARS 60 typedef struct { int x; int y; int speed; int speed2; int xpixle; unsigned short color; }Star; Star star[NUM_STARS]; void ClearScreen(void) { int i; for(i = 0; i < 256 * 192; i++) VRAM_B[i] = RGB15(0,0,0); } void InitStars(void) { int i; for(i = 0; i < NUM_STARS; i++) { star[i].color = RGB15(31,31,31); star[i].speed = rand() % 90 + 20; star[i].speed2 = 0; star[i].x = rand() % 256; star[i].y = rand() % 192; star[i].xpixle = 0; } } //code for drawing a line between two points void DrawLine(int x1, int y1, int x2, int y2, unsigned short color) { int yStep = SCREEN_WIDTH; int xStep = 1; int xDiff = x2 - x1; int yDiff = y2 - y1; int errorTerm = 0; int offset = y1 * SCREEN_WIDTH + x1; int i; if(xDiff < 0) { xDiff = -xDiff; xStep = -xStep; } if(yDiff < 0) { yDiff = -yDiff; yStep = -yStep; } if (xDiff > yDiff) { for(i = 0; i < xDiff + 1; i++) { VRAM_A[offset] = color; offset += xStep; errorTerm += yDiff; if (errorTerm > xDiff) { errorTerm -= xDiff; offset += yStep; } } } else { for(i = 0; i < yDiff + 1; i++) { VRAM_A[offset] = color; offset += yStep; errorTerm += xDiff; if (errorTerm > yDiff) { errorTerm -= yDiff; offset += xStep; } } } } void EraseStar(Star* star) { VRAM_B[(int)star->x + star->y * SCREEN_WIDTH] = RGB15(0,0,0); } void MoveStar(Star* star) { star->speed2 += star->speed; if(star->speed2 >= 100) { while(star->speed2 >= 100) { star->xpixle++; star->speed2 -= 100; } star->x += star->xpixle; star->xpixle = 0; } if(star->x >= SCREEN_WIDTH) { star->color = RGB15(31,31,31); star->x = 0; star->y = rand() % 192; star->speed = rand() % 90 + 20; } } void DrawStar(Star* star) { VRAM_B[(int)star->x + star->y * SCREEN_WIDTH] = star->color; } int main(void) { touchPosition touch; int oldX = 0; int oldY = 0; int i; // trying to set drawing(VRAM_A) to the bottom(main) screen and the moving stars(VRAM_B) to the top(sub). videoSetMode(MODE_FB0); videoSetModeSub(MODE_FB1); vramSetBankA(VRAM_A_LCD); vramSetBankB(VRAM_B_LCD); lcdMainOnBottom(); ClearScreen(); InitStars(); while(1) { scanKeys(); if(keysHeld() & KEY_START) { for(i = 0; i < 256 * 192; i++) { VRAM_A[i] = RGB15(0,0,0); } } touchRead(&touch); if(!(keysDown() & KEY_TOUCH) && (keysHeld() & KEY_TOUCH)) { DrawLine(oldX, oldY, touch.px, touch.py, rand()); } oldX = touch.px; oldY = touch.py; swiWaitForVBlank(); for(i = 0; i < NUM_STARS; i++) { EraseStar(&star[i]); MoveStar(&star[i]); DrawStar(&star[i]); } consoleClear(); } return 0; } |
With this program as it is, the drawing part works just fine, but the top screen displays nothing but white. I have no idea how to correct this issue. If anyone has suggestions, that would be just fantastic!
Anyway, these are my major concerns. Thanks for bearing with me, and I hope to have a good experience on these forums. :)